题解——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; }