HDOJ5281 Senior's Gun(贪心思想)

来源:互联网 发布:服务器数据库恢复 编辑:程序博客网 时间:2024/05/17 06:53

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.
 

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 nm.

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.

1n,m100000109a[i],b[j]109
 

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

Sample Input
12 22 32 2
 

Sample Output
1


a[i]b[j]最大即可


AC代码:

#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;typedef long long ll;bool cmp(ll a, ll b){return a > b;}int main(int argc, char const *argv[]){int t;scanf("%d", &t);while(t--) {ll n, m;scanf("%lld%lld", &n, &m);ll a[n + 1], b[m + 1];for(int i = 0; i < n; ++i)scanf("%lld", &a[i]);for(int i = 0; i < m; ++i)scanf("%lld", &b[i]);sort(a, a + n, cmp);sort(b, b + m);ll ans = 0, minx = min(n, m);for(int i = 0; i < minx; ++i)if(a[i] >= b[i]) ans += a[i] - b[i];else break;printf("%lld\n", ans);}return 0;}


1 0
原创粉丝点击