贪心/思维题 UVA 11292 The Dragon of Loowater

 

题目传送门

 1 /*  2  题意:n个头,m个士兵,问能否砍掉n个头  3  贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头  4 */  5 #include <cstdio>  6 #include <cstring>  7 #include <algorithm>  8 using namespace std;  9 10 const int MAXN = 2e4 + 10; 11 const int INF = 0x3f3f3f3f; 12 int a[MAXN], b[MAXN]; 13 14 int main(void) //UVA 11292 The Dragon of Loowater 15 { 16 // freopen ("A.in", "r", stdin); 17 18 int n, m; 19 while (scanf ("%d%d", &n, &m) == 2) 20  { 21 if (n == 0 && m == 0) break; 22 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); 23 for (int i=1; i<=m; ++i) scanf ("%d", &b[i]); 24 25 sort (a+1, a+1+n); 26 sort (b+1, b+1+m); 27 28 int ans = 0; 29 int i = 1, j = 1; 30 while (i <= n && j <= m) 31  { 32 if (a[i] <= b[j]) {ans += b[j]; i++; j++;} 33 else j++; 34  } 35 36 if (i == n + 1 && j <= m + 1) printf ("%d\n", ans); 37 else puts ("Loowater is doomed!"); 38  } 39 40 return 0; 41 } 42 43 /* 44 Loowater is doomed! 45 */

 

/*题意:n个头,m个士兵,问能否砍掉n个头    贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;
const int MAXN = 2e4 + 10;const int INF = 0x3f3f3f3f;int a[MAXN], b[MAXN];
int main(void)//UVA 11292 The Dragon of Loowater{//freopen ("A.in", "r", stdin);
int n, m;while (scanf ("%d%d", &n, &m) == 2){if (n == 0 && m == 0)break;for (int i=1; i<=n; ++i)scanf ("%d", &a[i]);for (int i=1; i<=m; ++i)scanf ("%d", &b[i]);
sort (a+1, a+1+n);sort (b+1, b+1+m);
int ans = 0;int i = 1, j = 1;while (i <= n && j <= m){if (a[i] <= b[j]){ans += b[j];i++; j++;}elsej++;}
if (i == n + 1 && j <= m + 1)printf ("%d\n", ans);elseputs ("Loowater is doomed!");}
return 0;}
/*Loowater is doomed!*/

 

你可能感兴趣的:(water)