Problem-solving ideas: This is a simple greedy problem, the ability of the knight high Price, if the ability to chop a very weak
's head is a waste of talent. So the hired Knights were able to sort from small to large, with all head diameters
According to the order from small to large, one by one to cut it. You can't cut off your current head without hiring.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intMAXN =20005;6 intA[MAXN], B[MAXN];7 intMain ()8 {9 intN, M, Sum, Cnt1, Cnt2;Ten while(~SCANF ("%d%d", &n, &m) && (n | |m)) One { A for(inti =0; I < n; i++) scanf ("%d", &a[i]); - for(inti =0; I < m; i++) scanf ("%d", &b[i]); - if(M < n)//because a knight can only chop one head, if the number of knights is less than the //The number of headers, the direct output can be. - { -printf"Loowater is doomed!\n"); - Continue; + } -Sort (A, a + N);//Direct sort sort, default from small to large +Sort (b, B +m); Acnt1 = Cnt2 = SUM =0; at while(1) - { - if(B[cnt2] >= a[cnt1])//If you can cut off the current header, the pointer - //move back one bit. - { -Sum + =B[cnt2]; inCnt2 + +, Cnt1 + +; - } to ElseCnt2 + +;//if not, the knight to the big pointer moves back one + - if(Cnt1 = = N | | cnt2 = = m) Break;//if any of the pointers have been scanned, jump out. the } * if(Cnt1 = = N) printf ("%d\n", sum);//If all the heads are cut, the output will be the least cost. $ Elseprintf"Loowater is doomed!\n");Panax Notoginseng } - return 0; the}
View Code
UVa11292 Dragon of Loowater