题目连接
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
贪心,贪心。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 using std::cin; 9 using std::cout; 10 using std::endl; 11 using std::find; 12 using std::sort; 13 using std::map; 14 using std::pair; 15 using std::vector; 16 using std::multimap; 17 #define pb(e) push_back(e) 18 #define sz(c) (int)(c).size() 19 #define mp(a, b) make_pair(a, b) 20 #define all(c) (c).begin(), (c).end() 21 #define iter(c) decltype((c).begin()) 22 #define cls(arr,val) memset(arr,val,sizeof(arr)) 23 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 24 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 26 const int N = 100010; 27 typedef long long ll; 28 int A[N], B[N]; 29 int main() { 30 #ifdef LOCAL 31 freopen("in.txt", "r", stdin); 32 freopen("out.txt", "w+", stdout); 33 #endif 34 ll ans; 35 int t, n, m, k, p; 36 scanf("%d", &t); 37 while (t--) { 38 ans = 0; 39 scanf("%d %d", &n, &m); 40 rep(i, n) scanf("%d", &A[i]); 41 rep(i, m) scanf("%d", &B[i]); 42 sort(A, A + n), sort(B, B + m); 43 k = n - 1, p = 0; 44 while (k > -1 && p < m && A[k] > B[p]) ans += A[k--] - B[p++]; 45 printf("%lld\n", ans); 46 } 47 return 0; 48 }