P10654 [ROI 2017] 水星上的服务器 (Day 2) 题解
模拟赛上做的一道题还是很简单的。
第一次交题解,恳请管理员大人通过!
解题思路
个点, 条边,第 条边连接第 和第 个点,时间限制为 。
显然是条链,考虑 dp。
以 点作为起点考虑。 到 与 到 对答案的贡献是相互独立的。而最终的答案就是两段答案集合的交集的最小值,如果为 ,就输出 。
不难看出可以设计两种 dp。
代表以 为起点最终能让 到 全部收到的初始时刻集合。
代表以 为起点最终能让 到 全部收到的初始时刻集合。
显然, 与 为 。
考虑转移,不妨假设这个 dp 中的答案集合是区间形式。设 为 ,
不考虑 ,则 ,考虑延时数组 ,如果 那么答案会扩展为 。
对于 ,转移与 一致,倒着枚举一遍就行。
码儿
不加优化,竟然跑了次优解!!!
#include<bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;
const int N=2e5+30,int e9=1e9+100;
int n,t[N];
struct as{int l,r;}dp1[N],dp2[N],e[N];
as jiao(as x,as y){
as ans;
int l1=x.l,l2=y.l,r1=x.r,r2=y.r;
l1=max(l1,l2);
r1=min(r1,r2);
if(l1<=r1 and l1>=0){
ans={l1,r1};
return ans;
}
ans={-1,-1};
return ans;
}
signed main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
freopen("server.in","r",stdin);
freopen("server.out","w",stdut);
cin>>n;
for(int i=1;i<=n;i++){cin>>t[i];}
for(int i=1;i<n;i++){
cin>>e[i].l>>e[i].r;
}
dp1[1]={0,e9};
for(int i=2;i<=n;i++){
dp1[i]=jiao(dp1[i-1],e[i-1]);
if(dp1[i].l>=0 and dp1[i].l<=e[i-1].l) dp1[i].l=max(0ll,dp1[i].l-t[i]);
}
dp2[n]={0,e9};
for(int i=n-1;i>=1;i--){
dp2[i]=jiao(dp2[i+1],e[i]);
if(dp2[i].l>=0 and dp2[i].l<=e[i].l) dp2[i].l=max(0ll,dp2[i].l-t[i]);
}
for(int i=1;i<=n;i++){
dp1[i]=jiao(dp1[i],dp2[i]);
cout<<dp1[i].l<<'\n';
}
return 0;
}