Xuejiejie is a beautiful and charming sharpshooter.
She often carries n n guns, and every gun has an attack power a[i] a[i] .
One day, Xuejiejie goes outside and comes across m m monsters, and every monster has a defensive power b[j] b[j] .
Xuejiejie can use the gun i i to kill the monster j j , which satisfies b[j]≤a[i] b[j]≤a[i] , and then she will get a[i]−b[j] a[i]−b[j] bonus .
Remember that every gun can be used to kill at most one monster, and obviously every monster can be killed at most once.
Xuejiejie wants to gain most of the bonus. It's no need for her to kill all monsters. Input
In the first line there is an integer
T
T
, indicates the number of test cases.
In each case:
The first line contains two integers n n , m m .
The second line contains n n integers, which means every gun's attack power.
The third line contains m m integers, which mean every monster's defensive power.
1≤n,m≤100000 1≤n,m≤100000 , −109≤a[i],b[j]≤109 −109≤a[i],b[j]≤109 。
Output
For each test case, output one integer which means the maximum of the bonus Xuejiejie could gain.
Sample Input
Sample Output
She often carries n n guns, and every gun has an attack power a[i] a[i] .
One day, Xuejiejie goes outside and comes across m m monsters, and every monster has a defensive power b[j] b[j] .
Xuejiejie can use the gun i i to kill the monster j j , which satisfies b[j]≤a[i] b[j]≤a[i] , and then she will get a[i]−b[j] a[i]−b[j] bonus .
Remember that every gun can be used to kill at most one monster, and obviously every monster can be killed at most once.
Xuejiejie wants to gain most of the bonus. It's no need for her to kill all monsters. Input
In each case:
The first line contains two integers n n , m m .
The second line contains n n integers, which means every gun's attack power.
The third line contains m m integers, which mean every monster's defensive power.
1≤n,m≤100000 1≤n,m≤100000 , −109≤a[i],b[j]≤109 −109≤a[i],b[j]≤109 。
1
2 2
2 3
2 2
1
将两个数组排序,一个降序,一个升序,这样就满足最大的攻击力打最小的守备力。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000+5;
typedef long long LL;
LL a[maxn],b[maxn];
bool cmp(LL a, LL b)
{
return a>b;
}
int main()
{
int T;
scanf("%d",&T);
int m,n;
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=0; i<m; i++)
scanf("%lld",&a[i]);
sort(a,a+m,cmp);
for(int i=0; i<n; i++)
scanf("%lld",&b[i]);
sort(b,b+n);
//int minn=n<m?n:m;
int minn=min(m,n);
LL sum=0;
for(int i=0; i<minn; i++)
{
if(a[i]>=b[i])
sum+=a[i]-b[i];
else
break;
}
printf("%lld\n",sum);
}
return 0;
}