题目链接
链接: https://codeforces.com/problemset/problem/1547/E.
思路1(思维171ms)
每一个点的温度,都有三种可能确定,1.本身的空调 2.从左边传过来 3.从右边传过来。所以从前向后过一遍最小值,再从后往前过一遍最小值就可以了
思路1代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 3e5 + 1e3;
const int inf = 0x3f3f3f3f;
int n , m , pos[N] , t[N];
int main(){
ios::sync_with_stdio(false);
int i , j , q , tmp;
cin>>q;
while(q--){
cin>>n>>m;
for(i = 1 ; i <= n ; i ++ ) t[i] = inf;
int minIndex = N , maxIndex = 0;
for(i = 1 ; i <= m ; i ++ ){
cin>>pos[i];
minIndex = min(minIndex , pos[i]);
maxIndex = max(maxIndex , pos[i]);
}
for(i = 1 ; i <= m ; i ++ ){
cin>>tmp;
t[pos[i]] = tmp;
}
for(i = minIndex+1 ; i <= n ; i ++ ){
t[i] = min(t[i],t[i-1]+1);
}
for(i = maxIndex-1 ; i >= 1 ; i -- ){
t[i] = min(t[i],t[i+1]+1);
}
for(i = 1 ; i <= n ; i ++ ){
cout<<t[i];
if(i!=n) cout<<" ";
}
cout<<endl;
}
return 0;
}
也可以用最短路解决:想法就是一个虚拟点链接所有空调点,然后求最短路。
还可以用线段树解决,不过我没听懂大佬讲的什么意思,搜csdn也没找到。。以后再说吧