题解 CF962E 【Byteland, Berland and Disputed Cities】
画画图可以发现 无非两种决策:
- PB依次连边(PR同理) 2.相邻两个P之间先连边(作为B和R 与P连通图的公用边)
然后相邻P之间的B和R单独连边(注意中间一条最大边可以去掉)
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ans,n,i,R,d,c,f,v,g,B,P;
int main(){
scanf("%I64d",&n);
for (int i=1;i<=n;i++){
ll m; char s;
scanf("%I64d %c",&m,&s);
if (s=='P'||s=='R'){
if (R) ans+=m-d,c=max(c,m-d);
R=1; d=m;
}
if (s=='P'||s=='B'){
if (B) ans+=m-f,v=max(v,m-f);
B=1; f=m;
}
if (s=='P'){
if (P) ans+=min(0ll,m-g-v-c);
P=1; c=v=0; g=m;
}
}
printf("%I64d\n",ans);
return 0;
}