题意:
给出两个序列,每个序列从中选出 k k k个进行配对,每一对的贡献就是 ∣ a i − b i ∣ |a_i-b_i| ∣ai−bi∣,求答案最大值
分析:
e m m emm emm一眼就感觉可以贪,于是把一个序列从小到大,另一个从大到小,每次都选择一对最极端的去配对就好啦
代码:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#define LL long long
using namespace std;
inline int read() {
int d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
LL a[100005],b[100005],c[100005];
bool cmp(LL x,LL y) {return x>y;}
int main()
{
LL n=read(),k=read();
for(LL i=1;i<=n;i++) a[i]=read();
for(LL i=1;i<=n;i++) b[i]=read();
sort(a+1,a+1+n);sort(b+1,b+1+n,cmp);
for(LL i=1;i<=n;i++) c[i]=abs(a[i]-b[i]);
sort(c+1,c+1+n,cmp);
LL ans=0;
for(LL i=1;i<=k;i++) ans+=c[i];
cout<<ans;
return 0;
}