王道机试—例题 7.3 Senior’s Gun(简单贪心03)

题目描述:

Xuejiejie is a beautiful and charming sharpshooter. She often carries n guns, and every gun has anattack 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.

输入:

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.

输出:

For each test case, output one integer which means the maximum of the bonus Xuejiejie could gain.

样例输入

1

2 2

2 3

2 2

样例输出

1

题目大意

薛杰杰是一位美丽迷人的神枪手。她经常携带 n 支枪,每支枪都具有攻击力 a[i]。 有一天,薛杰杰走到外面,遇到了怪物,每个怪物都有防御力 b[j]。 在满足 b[j] a[i]的情况下,薛杰杰可用 i 枪杀死怪物 j,她会得到 a[i] − b[j]的奖金。

请记住,每支枪只能用来杀死一个怪物,而且显然每个怪物只能被杀死一次。

薛杰杰想最大化她的奖金,并且她没有必要杀死所有的怪物。

分析

本题的贪心策略不是很明显,每支枪只能用一次,而且要使总奖金最大,就需要在 a[i] > b[i]的情况下,a[i]的值尽量大而 b[i]的值尽量小,于是可以想到一直用剩下的枪中攻击力最强的去干掉剩下怪物中防御力最弱的,以便让每一枪都能获得当前情况下最大的奖金。

因此关键点是,用两个sort排序即可,gun[i]降序排序;m[i]升序排序;

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务