题解——Codeforces Round #508 (Div. 2) T3 (贪心)
Posted dreagonm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解——Codeforces Round #508 (Div. 2) T3 (贪心)相关的知识,希望对你有一定的参考价值。
贪心的选取最优解
然后相减好
记得要开long long
#include <cstdio> #include <algorithm> #include <cstring> #include <set> #include <queue> #define int long long using namespace std; int ansa=0,ansb=0,posa=1,posb=1,n,a[1000100],b[1000100]; bool cmp(int a,int b){ if(a>b) return true; else return false; } signed main(){ scanf("%I64d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); for(int i=1;i<=n;i++) scanf("%I64d",&b[i]); sort(a+1,a+n+1,cmp); sort(b+1,b+n+1,cmp); int whi=0;// 0 a 1 b while(posa<=n||posb<=n){ if(whi==0){ if(a[posa]>b[posb]&&posa<=n){ ansa+=a[posa]; posa++; whi=1-whi; } else if(a[posa]<=b[posb]&&posb<=n){ posb++; whi=1-whi; } else if(posb<=n){ posb++; whi=1-whi; } else if(posa<=n){ ansa+=a[posa]; posa++; whi=1-whi; } } else{ if(b[posb]>a[posa]&&posb<=n){ ansb+=b[posb]; posb++; whi=1-whi; } else if(b[posb]<=a[posa]&&posa<=n){ posa++; whi=1-whi; } else if(posa<=n){ posa++; whi=1-whi; } else if(posb<=n){ ansb+=b[posb]; posb++; whi=1-whi; } } } printf("%I64d",ansa-ansb); return 0; }
以上是关于题解——Codeforces Round #508 (Div. 2) T3 (贪心)的主要内容,如果未能解决你的问题,请参考以下文章
题解——Codeforces Round #508 (Div. 2) T2 (构造)
Codeforces Round #508 (Div. 2)
Codeforces Round #815 (Div. 2) 题解
Codeforces Round #805 (Div. 3) 题解