贪心的选取最优解

然后相减好

记得要开long long

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <queue>
#define int long long
using namespace std;
int ansa=,ansb=,posa=,posb=,n,a[],b[];
bool cmp(int a,int b){
if(a>b)
return true;
else
return false;
}
signed main(){
scanf("%I64d",&n);
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]);
for(int i=;i<=n;i++)
scanf("%I64d",&b[i]);
sort(a+,a+n+,cmp);
sort(b+,b+n+,cmp);
int whi=;// 0 a 1 b
while(posa<=n||posb<=n){
if(whi==){
if(a[posa]>b[posb]&&posa<=n){
ansa+=a[posa];
posa++;
whi=-whi;
}
else if(a[posa]<=b[posb]&&posb<=n){
posb++;
whi=-whi;
}
else if(posb<=n){
posb++;
whi=-whi;
}
else if(posa<=n){
ansa+=a[posa];
posa++;
whi=-whi;
}
}
else{
if(b[posb]>a[posa]&&posb<=n){
ansb+=b[posb];
posb++;
whi=-whi;
}
else if(b[posb]<=a[posa]&&posa<=n){
posa++;
whi=-whi;
}
else if(posa<=n){
posa++;
whi=-whi;
}
else if(posb<=n){
ansb+=b[posb];
posb++;
whi=-whi;
}
}
}
printf("%I64d",ansa-ansb);
return ;
}
05-01 22:26