Test instructions: There are n Dragon, in the mercenary warrior to kill, each warrior ability value is X, can only kill the head of the diameter y is less than or equal to their ability value of the dragon, can only be hired once, and you have to give X bounty, the least reward.
Analysis: Very simple, it is obvious that the ability to kill a large diameter, low kill diameter small. So we first order the warrior ability from small to large, and then the diameter of the dragon from small to large sort,
And then sweep once, such as a warrior killed the dragon, you can skip, sweep to the end, if the killing is over, the output costs, otherwise is not killed.
The code is as follows:
#include <iostream> #include <cstdio> #include <climits> #include <cmath> #include < algorithm> #include <cstring> #include <map>using namespace std;const int maxn = 20000 + 10;int A[MAXN], B[ma Xn];int Main () { int n, m; while (scanf ("%d%d", &n, &m) && m && N) { for (int i = 0; i < n; ++i) scanf ("%d", &a[ I]); for (int i = 0; i < m; ++i) scanf ("%d", &b[i]); Sort (A, a+n); Sort (b, b+m); if (n > m) { printf ("Loowater is doomed!\n"); Continue; } Warriors too little, directly ending int cnt = 0, cost = 0; for (int i = 0; l < m; ++i) if (B[i] >= a[cnt]) { Cost + = b[i];//Write down charges if (++cnt = = N) break;//the Dragon is dead, Tim. Before exiting } if (CNT < n) printf ("Loowater is doomed!\n"); else printf ("%d\n", cost); } return 0;}
UVa 11292 Dragon of Loowater (water problem, sort)