CF493B 题解

发布时间 2024-01-01 20:59:09作者: zhuangjihong

好久没写题解了

思路

统计两个选手的分数,如果不是相同的就直接比大小,否则按字典序的大小,注意这里的字典序是两个选手的分数拼起来,比如样例 \(1\)

5
1
2
-3
-4
3

那么两个选手分别是 12334。如果字典序相同,就看最后一个得分的选手是谁谁就赢。

代码

#include<iostream>
using namespace std;
const int MAXN=200005;
long long a[MAXN],b[MAXN];
int main()
{
    int n,ai=0,bi=0,last;//last是最后一个选手是谁
    long long as=0,bs=0;//忘记开longlong了T_T
    cin>>n;
    for(int i=1;i<=n;i++){
        long long x;
        cin>>x;
        if(x>0){
            ai++;
            a[ai]=x;
            as+=x;
        }
        else{
            bi++;
            b[bi]=-x;
            bs-=x;
        }
        if(i==n){
            if(x>0) last=1;
            else last=0;
        }
    }
    if(as>bs) cout<<"first";//判断总分数
    else if(as<bs) cout<<"second";
    else{
        for(int i=1;i<=min(ai,bi);i++){//判断字典序
            if(a[i]>b[i]){
                cout<<"first";
                return 0;
            }
            else if(a[i]<b[i]){
                cout<<"second";
                return 0;
            }
        }
        if(ai>bi) cout<<"first";
        else if(ai<bi) cout<<"second";
        else if(last) cout<<"first";//判断最后一个选手
        else cout<<"second";
    }
    return 0;
}