比赛 进阶指南第0章测试 评测结果 AAAWWWWWWW
题目名称 有n种物品 最终得分 30
用户昵称 rzzakioi 运行时间 0.136 s
代码语言 C++ 内存使用 2.06 MiB
提交时间 2026-03-14 10:56:32
显示代码纯文本
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
int n,ans1,ans2;
struct node{
    int l,r;
    bool operator <(const node &other)const{
        if(l==other.l)return r<other.r;
        return l>other.l;
    }
}a[100005];
signed main(){
    freopen("nit.in","r",stdin);
    freopen("nit.out","w",stdout);
    scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld%lld",&a[i].l,&a[i].r);
    }
    sort(a+1,a+n+1);
    bool flag=0;
    for(int i=1;i<=n;i++){
        if(a[i].l>=a[i].r){
            flag=!flag;
            if(flag){
                ans1+=a[i].l;
                ans2+=a[i].r;
            }
            else{
                ans2+=a[i].l;
                ans1+=a[i].r;
            }
        }
        else{
            ans1+=a[i].l;
            ans2+=a[i].r;
        }
    }
    printf("%lld",ans1-ans2);
    return 0;
}