B - RGB Matching(模拟+细节)

这篇博客讨论了一种编程竞赛的题目,涉及到将不同颜色的球分配到两个组中,使得组内颜色球之间的差值最小。作者使用了二分搜索策略,并详细解释了如何在奇数数量的球中决定分配,以及如何更新最优解。博客还包含了完整的C++代码实现。
摘要由CSDN通过智能技术生成

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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值