UVA 11292 Dragon of Loowater

#include<stdio.h>
#include<stdlib.h>

#define MAX 20000
int headHight[MAX + 1];
int knightHight[MAX + 1];

int cmp(const void *a, const void *b){
	return *(int *)a - *(int *)b;
}

int main()
{
    
	int numOfHeads, numOfKnights;
	while (scanf("%d%d", &numOfHeads, &numOfKnights) != EOF){
		if (numOfHeads == 0 && numOfKnights == 0)
			return 0;
		int head;
		for (head = 1; head <= numOfHeads; head++)
			scanf("%d", &headHight[head]);
		int knight;
		for (knight = 1; knight <= numOfKnights; knight++)
			scanf("%d", &knightHight[knight]);

		qsort(&headHight[1], numOfHeads, sizeof(headHight[0]), cmp);
		qsort(&knightHight[1], numOfKnights, sizeof(knightHight[0]), cmp);

		knight = 1;
		int fail = 0;
		int pay = 0;
		for (head = 1; head <= numOfHeads; head++){
			while (knight <= numOfKnights && knightHight[knight] < headHight[head])
				knight++;
			if (knight > numOfKnights){
				fail = 1;
				break;
			}
			pay += knightHight[knight];
			knight++;
		}

		if (fail)
			printf("Loowater is doomed!\n");
		else
			printf("%d\n", pay);

	}
    return 0;
}

你可能感兴趣的:(uva,of,dragon,11292,Loowater)