Senior's Gun
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1608 Accepted Submission(s): 558
Problem Description
Xuejiejie is a beautiful and charming sharpshooter.
She often carries n
guns, and every gun has an attack power
a[i]
.
One day, Xuejiejie goes outside and comes across m
monsters, and every monster has a defensive power
b[j]
.
Xuejiejie can use the gun i
to kill the monster
j
, which satisfies
b[j]≤a[i]
, and then she will get
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.
She often carries n
One day, Xuejiejie goes outside and comes across m
Xuejiejie can use the gun i
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
, indicates the number of test cases.
In each case:
The first line contains two integers n
,
m
.
The second line contains n
integers, which means every gun's attack power.
The third line contains m
integers, which mean every monster's defensive power.
1≤n,m≤100000
,
−10
9
≤a[i],b[j]≤10
9![]()
。
In each case:
The first line contains two integers n
The second line contains n
The third line contains m
1≤n,m≤100000
Output
For each test case, output one integer which means the maximum of the bonus Xuejiejie could gain.
Sample Input
1 2 2 2 3 2 2
Sample Output
1
虽然是稍微有点水的贪心题目,但是看看我的博客里边相关贪心的题解实在是少的可怜,这里就写出来供大家一起探讨。
题意还是比较好理解的,这里有一个小小的坑点:枪的数量,和怪物的数量,不一定是相等的,所以我们这里要特殊处理一下。
思路还是很好想通的,比田忌赛马相关问题简单了不少。没有那样复杂,我们这里直接拿最牛逼的枪干最差的怪物就行了:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int gun[100010];
int xue[101010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&gun[i]);
}
for(int i=0;i<m;i++)
{
scanf("%d",&xue[i]);
}
sort(gun,gun+n);
reverse(gun,gun+n);//反向处理的方法之一~
sort(xue,xue+m);
long long int sum=0;
int minn=min(n,m);
for(int i=0;i<minn;i++)
{
if(gun[i]>xue[i])
sum+=gun[i]-xue[i];
}
printf("%lld\n",sum);
}
}