hdu 5281 Senior's Gun

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5281

Senior's Gun

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]\leq 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 $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\leq n, m\leq 100000$, $-10^9 \leq a[i], b[j]\leq 10^9$。

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<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef long long ll;
int A[N], B[N];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
ll ans;
int t, n, m, k, p;
scanf("%d", &t);
while (t--) {
ans = ;
scanf("%d %d", &n, &m);
rep(i, n) scanf("%d", &A[i]);
rep(i, m) scanf("%d", &B[i]);
sort(A, A + n), sort(B, B + m);
k = n - , p = ;
while (k > - && p < m && A[k] > B[p]) ans += A[k--] - B[p++];
printf("%lld\n", ans);
}
return ;
}
上一篇:OC-Objection 学习笔记之一:简单的开始


下一篇:javascript解析器原理