描述 Description | |||
给出2个序列A={a[1],a[2],…,a[n]},B={b[1],b[2],…,b[n]},从A、B中各选出n个元素进行一一配对(可以不按照原来在序列中的顺序),并使得所有配对元素差的绝对值之和最大。 | |||
输入格式 Input Format | |||
输入的第1行为1个整数n 第2行包含n个整数,题目中的A序列。 第3行包含n个整数,题目中的B序列。 | |||
输出格式 Output Format | |||
一个数,最大配对 | |||
样例输入 Sample Input [复制数据] | |||
样例输出 Sample Output [复制数据] | |||
时间限制 Time Limitation | |||
各个测试点1s |
我目测是排序不等式(但是没学这个,只是感觉是这样,好烦啊,我该自学一下的。),然后做,碰对了。
后来看了下书,书上的和这个不太一样,不知道是怎么证明出来的。。反正我一直觉得不等式是个牛逼的东西。
#include <functional>
#include <algorithm>
#include <cstdio>
#include <cmath>
using std::sort;
long a[10010];
long b[10010];
int main()
{
freopen("match.in","r",stdin);
freopen("match.out","w",stdout);
long n;
scanf("%ld",&n);
for (long i=1;i<n+1;i++)
scanf("%ld",a+i);
for (long i=1;i<n+1;i++)
scanf("%ld",b+i);
sort(a+1,a+1+n,std::greater<long>());
sort(b+1,b+1+n,std::less<long>());
long ans = 0;
for (long i=1;i<n+1;i++)
{
ans += abs(a[i]-b[i]);
}
printf("%ld",ans);
return 0;
}