E. Air Conditioners

可以图论虚拟点做,也可以直接DP

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define int long long
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int gcd(int a,int b){return b?a:gcd(b,a%b);}
int lcm(int a,int b){return a*b/gcd(a,b);}
int qmi(int a,int b,int mod){int res=1;while(b){if(b&1)res=res*a%mod;b>>=1;a=a*a%mod;}return res;}


int n,q,m;
int pos[N],temp[N];
// int e[N],ne[N],w[N],h[N],idx;
// void add(int a,int b,int c){
	// e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
// }
int dp[N];

void solve()
{
	cin>>n>>m;
	for(int i=0;i<=n+1;i++)dp[i] = 0x3f3f3f3f;
	for(int i=1;i<=m;i++)cin>>pos[i];
	for(int i=1;i<=m;i++)cin>>temp[i];
	
	for(int i=1;i<=m;i++)dp[pos[i]] =  temp[i];
	
	for(int i=1;i<=n;i++)dp[i] = min(dp[i],dp[i-1]+1);
	for(int i=n;i>=1;--i)dp[i] = min(dp[i],dp[i+1]+1);
	
	for(int i=1;i<=n;i++)cout<<dp[i]<<" \n"[i==n];
	
	

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	cin>>_;
	//_ = 1;
	while(_--)solve();
	return 0;
}
cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define int long long
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int gcd(int a,int b){return b?a:gcd(b,a%b);}
int lcm(int a,int b){return a*b/gcd(a,b);}
int qmi(int a,int b,int mod){int res=1;while(b){if(b&1)res=res*a%mod;b>>=1;a=a*a%mod;}return res;}


int n,q,m;
int pos[N],temp[N];
// int e[N],ne[N],w[N],h[N],idx;
// void add(int a,int b,int c){
	// e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
// }
int dp[N];

void solve()
{
	cin>>n>>m;
	for(int i=0;i<=n+1;i++)dp[i] = 0x3f3f3f3f;
	for(int i=1;i<=m;i++)cin>>pos[i];
	for(int i=1;i<=m;i++)cin>>temp[i];
	
	for(int i=1;i<=m;i++)dp[pos[i]] =  temp[i];
	
	for(int i=1;i<=n;i++)dp[i] = min(dp[i],dp[i-1]+1);
	for(int i=n;i>=1;--i)dp[i] = min(dp[i],dp[i+1]+1);
	
	for(int i=1;i<=n;i++)cout<<dp[i]<<" \n"[i==n];
	
	

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	cin>>_;
	//_ = 1;
	while(_--)solve();
	return 0;
}
相关推荐
Zach_yuan3 分钟前
算法1111
算法
nvd117 分钟前
python 后端流式处理 LLM 响应数据详解
开发语言·python
蓝天智能8 分钟前
Qt 的字节序转换
开发语言·qt
不穿格子的程序员32 分钟前
从零开始刷算法——二分-搜索旋转排序数组
数据结构·算法
CS_浮鱼39 分钟前
【C++进阶】智能指针
开发语言·c++
怕什么真理无穷1 小时前
C++_面试题_21_字符串操作
java·开发语言·c++
做怪小疯子1 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
做怪小疯子1 小时前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
yi碗汤园1 小时前
【一文了解】C#反射
开发语言·unity·c#
高山上有一只小老虎1 小时前
等差数列前n项的和
java·算法