配对
描述 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
4
2 5 6 3
1 4 6 7
样例输出 Sample Output
14
时间限制 Time Limitation
各个测试点1s
注释
Hint 3与6配对,2与7配对,5与4配对,6与1配对,绝对值之差和为14
对于10%的数据,有n≤20;
对于30%的数据,有n≤100;
对于50%的数据,有n≤1000;
对于100%的数据,有n≤10000;a[i],b[i]≤1000
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main(int ac,char *av[])
{
int n,i,sum,a[10000],b[10000];
while(cin>>n)
{
sum=0;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cin>>b[i];
sort(a,a+n);
sort(b,b+n);
for(i=0;i<n;i++)
sum+=abs(a[i]-b[n-1-i]);
cout<<sum<<endl;
}
return 0;
}