https://atcoder.jp/contests/arc121/tasks/arc121_b
思路:
最近状态很差。过会儿把下午补的cf发上来。
二分不用说。就是考虑偶数给奇数的时候,要考虑给第一个和第二个奇数哪个组更优,再来更新答案。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL r[maxn],g[maxn],b[maxn];
LL tot1,tot2,tot3;
LL p1[maxn],p2[maxn],cnt1,cnt2;
LL p3[maxn],cnt3;
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
for(LL i=1;i<=n*2;i++){
char col;
LL val;
cin>>val>>col;
if(col=='R') r[++tot1]=val;
if(col=='G') g[++tot2]=val;
if(col=='B') b[++tot3]=val;
}
if((tot1%2==0)&&(tot2%2==0)&&(tot3%2==0)){
cout<<"0"<<"\n";
return 0;
}
bool flag=1;LL ou=0;
if(tot1&1){
flag=0;
for(LL i=1;i<=tot1;i++){
p1[++cnt1]=r[i];
}
}
if(flag==0){
if(tot2&1){
for(LL i=1;i<=tot2;i++){
p2[++cnt2]=g[i];
}
ou=tot3;
for(LL i=1;i<=ou;i++){
p3[++cnt3]=b[i];
}
}
else{
for(LL i=1;i<=tot3;i++){
p2[++cnt2]=b[i];
}
ou=tot2;
for(LL i=1;i<=ou;i++){
p3[++cnt3]=g[i];
}
}
}
else{
ou=tot1;
for(LL i=1;i<=tot2;i++){
p1[++cnt1]=g[i];
}
for(LL i=1;i<=tot3;i++){
p2[++cnt2]=b[i];
}
for(LL i=1;i<=ou;i++){
p3[++cnt3]=r[i];
}
}
sort(p1+1,p1+1+cnt1);
sort(p2+1,p2+1+cnt2);
sort(p3+1,p3+1+cnt3);
LL ans=1e18;
for(LL i=1;i<=cnt1;i++){
LL pos1=lower_bound(p2+1,p2+1+cnt2,p1[i])-p2;
LL pos2=lower_bound(p2+1,p2+1+cnt2,p1[i])-p2;
pos1=min(pos1,cnt2);
pos2=max(1LL,pos2-1);
ans=min(ans,abs(p1[i]-p2[pos1]));
ans=min(ans,abs(p1[i]-p2[pos2]));
}
LL tmp1=1e18;LL tmp2=1e18;
for(LL i=1;i<=cnt3;i++){
LL pos1=lower_bound(p1+1,p1+1+cnt1,p3[i])-p1;
LL pos3=lower_bound(p1+1,p1+1+cnt1,p3[i])-p1;
pos1=min(pos1,cnt1);
pos3=max(1LL,pos3-1);
LL tmp3=min(abs(p3[i]-p1[pos1]),abs(p3[i]-p1[pos3]));
LL pos2=lower_bound(p2+1,p2+1+cnt2,p3[i])-p2;
LL pos4=lower_bound(p2+1,p2+1+cnt2,p3[i])-p2;
pos2=min(pos2,cnt2);
pos4=max(1LL,pos4-1);
LL tmp4=min(abs(p3[i]-p2[pos2]),abs(p3[i]-p2[pos4]));
///给第一个组
LL temp1=min(tmp1,tmp3);
LL ans1=temp1+tmp2;
///给第二个组
LL temp2=min(tmp2,tmp4);
LL ans2=temp2+tmp1;
if(ans1<ans2) ans=min(ans,ans1),tmp1=min(tmp1,temp1);
else ans=min(ans,ans2),tmp2=min(tmp2,temp2);
}
cout<<ans<<"\n";
return 0;
}