描述 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

code
#include<iostream>
#include<algorithm>
using namespace std;
const int N(10005);
int a[N],b[N];
int main()
{
int n,i,ans;
cin>>n;
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,greater<int>());
for (ans=i=0;i<n;i++)
ans+=abs(b[i]-a[i]);
cout<<ans<<endl;
//system("pause");
return 0;
}
posted on 2012-08-13 18:47
龙在江湖 阅读(212)
评论(0) 编辑 收藏 引用 所属分类:
竞赛题解_TYVJ 、
贪心