当前位置:网站首页>CF1547E Air Conditioners
CF1547E Air Conditioners
2022-04-22 07:26:00 【INGg__】
I read several solutions to this problem , I also feel that I have learned some knowledge
The question
Let's start with the meaning of the title
The length is n In a one-dimensional space line , Given k An air conditioner , At the same time, its position and set temperature are given . Ask the temperature of each cell .
Temperature calculation formula :
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∣),
The minimum value of the set temperature of all air conditioners plus the distance to this point is the temperature of this point
practice
First of all, I think the first method is the most applicable , At the same time, it can also correspond to the multi-source that I simply learned some time ago BFS
Take each air conditioner as a starting point , Join the queue at the same time , Update the adjacent points with the currently known points . The answer to each point can actually be based on the temperature on the left and right sides , To get , Through the formula, we know , You can add the increased distance to the minimum value of the left and right points 1 Compare with the current answer to this point and take the smallest
#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;
}
}
Source of ideas :Messywind
https://blog.csdn.net/messywind/article/details/118653468?spm=1001.2014.3001.5501
According to the bold words above , We can actually know , The answer of each point is only updated by the answers determined at the left and right ends , Or left +1 influence , Or by the right +1 influence , This constitutes a linear DP problem
#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;
}
}
Source of ideas :https://blog.csdn.net/weixin_50909982/article/details/118895522
版权声明
本文为[INGg__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613560816.html
边栏推荐
- 2021学习计划
- 【题解】洛谷P6186 [NOI Online #1 提高组] 冒泡排序:【冒泡排序】与【逆序对】问题
- Escape analysis in JVM can realize that memory is not allocated on the heap
- Leetcode - 4 - (longest substring without repeated characters, candy distribution, binary tree traversal)
- LaTex用模板的时候图片的caption标题无法左对齐
- 手撕算法---LRU缓存淘汰策略,问的这么频繁
- Codeforces Round #774 (Div. 2)
- [DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
- (2) Basic configuration of SQL server and connection to SQL server using Navicat
- Quartus II prevents signals from being integrated
猜你喜欢

L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)

Byte Summer Internship - 20220304

B.Cutting Corners (简单几何/签到)(2021年度训练联盟热身训练赛第五场)

In the process of class loading, the allocation area of class variables is different from that of instance variables

带环链表详解

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

Beyond compare solution to "authorization key has been revoked"

When latex uses the template, the caption title of the picture cannot be left aligned

Unknown graphics extension:. 1 jpg. }

抽象类和抽象方法
随机推荐
最强操作符学习之路(C语言详解)
系统日志收集系列
Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)
The thought and code of eight sorting
[number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
微软实习生面试的2道算法题目——20220119
Virtual machine disk space shrinks
SecureCRT infinite loop script
C.Ducky Debugging(简单判断/签到)(2021年度训练联盟热身训练赛第五场 )
[number theory] congruence (I): basic concepts and properties of congruence
Codeforces Round #778
(5) Use Navicat to create database data table and set ID auto increment
Anaconda installation and use
Educational Codeforces Round 125 (Rated for Div. 2)
Final关键字
189. 轮转数组
[number theory] prime number (I): basic concepts, properties, conjectures and theorems
详解冒泡序列与数组名
SQL Server quick start
E.Figure Skating (字符串排序/签到) (2021年度训练联盟热身训练赛第五场 )