当前位置:网站首页>CF1547E Air Conditioners
CF1547E Air Conditioners
2022-04-22 06:16:00 【INGg__】
这道题看了几篇题解,也感到学到了一些知识
题意
先说题意
长度为 n 的一个一维的空间直线中,给定 k 个空调,同时给定其位置与设定的温度。问每一个格的温度分别是多少。
温度计算公式:
min 1 ≤ j ≤ k ( t j + ∣ a j − i ∣ ) , \min_{1 \le j \le k}(t_j + |a_j - i|), 1≤j≤kmin(tj+∣aj−i∣),
就是所有空调设定的温度加上到这个点的距离的最小值就是这个点的温度
做法
首先第一个做法我觉得是适用性最高的,也同时能够对应好我前段时间简单学的多源BFS
把每个空调当做一个起点,同时加入到队列中去,用当前已知到的点去更新相邻的点。每个点的答案实际上可以根据左右两边的温度,来进行得到,通过公式我们知道,可以通过左右两个点此时的最小值加上增加的距离与1与当前的知道的这个点的答案进行比较取最小
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define pb push_back
#define Endl "\n"
#define endl "\n"
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int dx[4] = {
-1,0,1,0};
int dy[4] = {
0,1,0,-1};
int T;
int n, k;
int a[N];
int t[N];
int dis[N];
int main(){
cin >> T;
while(T--){
memset(dis, 0x3f, sizeof(dis));
priority_queue<PII, vector<PII>, greater<PII>> q;
cin >> n >> k;
for (int i = 1; i <= k; i++){
cin >> a[i];
}
for (int i = 1; i <= k; i++){
cin >> t[i];
q.push({
a[i], t[i]});
}
while(!q.empty()){
auto t = q.top();
q.pop();
if(t.y < dis[t.x]){
dis[t.x] = t.y;
if(t.x + 1 <= n)
q.push({
t.x + 1, t.y + 1});
if(t.x - 1 >= 1)
q.push({
t.x - 1, t.y + 1});
}
}
for (int i = 1; i <= n; i++){
cout << dis[i] << " ";
}
cout << Endl;
}
}
思路来源:Messywind
https://blog.csdn.net/messywind/article/details/118653468?spm=1001.2014.3001.5501
根据上面的加粗的字,我们实际上可以就知道,每一个点的答案只是由左右两端确定的答案来进行更新的,要么被左边+1影响,要么被右边+1影响,这就构成了一个线性DP问题
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define pb push_back
#define Endl "\n"
#define endl "\n"
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int dx[4] = {
-1,0,1,0};
int dy[4] = {
0,1,0,-1};
int T;
int n, k;
int a[N];
int t[N];
int main(){
cin >> T;
while(T--){
memset(t, 0x3f, sizeof(t));
cin >> n >> k;
for (int i = 1; i <= k; i++){
scanf("%d", &a[i]);
}
for (int i = 1; i <= k; i++){
int x;
cin >> x;
t[a[i]] = x;
}
for (int i = 1; i <= n; i++)
t[i] = min(t[i - 1] + 1, t[i]);
for (int i = n; i >= 1; i--)
t[i] = min(t[i + 1] + 1, t[i]);
for (int i = 1; i <= n; i++)
cout << t[i] << " ";
cout << Endl;
}
}
思路来源:https://blog.csdn.net/weixin_50909982/article/details/118895522
版权声明
本文为[INGg__]所创,转载请带上原文链接,感谢
https://blog.csdn.net/INGg__/article/details/120047101
边栏推荐
- SQL review, grammar notes, fresh out
- Redis進階
- The thought and code of eight sorting
- Jenkins deployment PM2
- 189. 轮转数组
- L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)
- synchronized锁优化的一些机制(锁升级)
- Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
- Prompt the user to enter his name, and the user will write his name to the file guest Txt program determines that when it is not equal to N, it executes to create the file data Txt, a total of 100000
- What is the internal structure of stack frame?
猜你喜欢

最强操作符学习之路(C语言详解)

LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)

链表习题详解

LaTex中添加作者照片和作者简介

带环链表详解

(2) Basic configuration of SQL server and connection to SQL server using Navicat

Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)

Byte Summer Internship - 20220304

Define a student class 1 to get the student's name: get_ Name() return type: STR 2 get student's age: get_ Age() return type: int 3 returns the highest score among the three subjects. get_ course()

Unknown graphics extension:. 1 jpg. }
随机推荐
a5 transceiver 信号vod和预加重调整关系
idea 不显示Run Dashboard视图窗口的问题
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
If an error is reported, process the ES6 filter to filter the array
The problem of interval summation -- difference
[number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
Vscode, this is enough
Relationship between A5 transceiver signal VOD and pre emphasis adjustment
Unknown graphics extension:. 1 jpg. }
详解树状数组模板——理论和代码的实现
What is socket programming?
MacOS installs redis and sets the service self start
使用Feign做远程调用的注意点
L2-005 集合相似度(set判重)
抽象类和抽象方法
[number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
Jenkins deployment PM2
Complete a student information management system and systematically practice object-oriented, function, string and other knowledge. Realize the comprehensive application of knowledge. Use classes, fun
Beyond Compare“授权密钥已被吊销”的解决办法
Win10 Anaconda install cocotb