Codeforces Round 1008 (Div. 2) (C-D)

后面更新E

Problem - C - Codeforces

思路

可以发现题意中的其中一个条件其实是奇数项和等于偶数项和

于是我们可以把2n个数排序后,取前面的做奇数项,和为\(pre\),后面的做偶数项,和\(suf\),那么\(a_1\)就是\(suf-pre\)就满足该条件

但是仅仅这样却不一定满足两两不同的条件,也就是出现了前面得到的\(a_1\)等于给定的\(2n\)个数的情况
处理方法很简单,我们让前\(n-1\)个数加上\(suf-pre\)做奇数项,在\([n,n*2-1]\)的数作偶数项,最后一个数作\(a_1\)即可

比如最后一个样例

代码

void solve(){
    int n,k;cin>>n;
    vector<int>a(n*2+1);
    rep(i,1,n*2) cin>>a[i];
    sort(a.begin()+1,a.end());
    int pre=0,suf=0;
    rep(i,1,n) pre+=a[i];
    per(i,n*2,n+1) suf+=a[i];
    pre-=a[n];
    suf+=a[n];
    int tmp=suf-pre;
    cout<<a[n*2]<<" ";
    vector<int>b(n*2+1);
    int j=1,s=n,f=0;
    rep(i,1,n*2){
        if(i&1){
            if(f==0) b[i]=tmp,f=1;
            else b[i]=a[j],j++;
        } 
        else b[i]=a[s],s++;
    }
    _db(b,n*2);
}

Problem - D - Codeforces

思路

贪心,对于都是加或者都是乘的情况,放在哪一侧在当前步对答案的贡献都是一样的,但是却可能导致在以后不优
所有不如把此处的贡献暂存起来,等遇到别的情况需要时再决策放在当前步更优的一侧

Code

struct node{
    char c1;
    int a;
    char c2;
    int b;
};
void solve(){
    int n;cin>>n;
    vector<node>p(n+1);
    rep(i,1,n){
        char c1,c2;
        int a,b;
        cin>>c1>>a>>c2>>b;
        p[i]={c1,a,c2,b};
    }
    int l=1,r=1,now=0;
    rep(i,1,n){
        auto rt=p[i];
        int f=0;
        if(rt.c1=='x'&&rt.c2=='x'){
            if(rt.a>rt.b) l+=now;
            else if(rt.a<rt.b) r+=now;
            else{
                f=1;
                now=(l+r)*(rt.a-1)+now*rt.a;
            }
        }
        else if(rt.c1=='x') l+=now;
        else if(rt.c2=='x') r+=now;
        else{
            f=1;
            now+=rt.a+rt.b;
        }
        if(f) continue;
        now=0;
        if(rt.c1=='x') now+=(rt.a-1)*l;
        else now+=rt.a;
        if(rt.c2=='x') now+=(rt.b-1)*r;
        else now+=rt.b;
        // cout<<l<<" "<<r<<" "<<now<<"\n";
    }
    int ans=l+r+now;
    cout<<ans<<"\n";
}
posted @ 2025-03-11 11:00  mono_4  阅读(169)  评论(0)    收藏  举报