UVA11292 The Dragon of Loowater

题意: 国王要杀恶龙, 需要雇佣骑士。 每个骑士只能杀一条龙。 当骑士身高 不小于 龙头时, 骑士可以砍下龙头。 雇佣骑士需要钱, 钱就等于他的身高。 如果骑士能成功砍下所有的龙头, 则输出雇佣总费最少的数值, 否则输出那句鸟语。

思路: 排序+贪心。 先把龙头跟骑士身高各自从小到大排序, 然后从头到尾比较。 若能砍下则雇佣, 否则用下一个骑士。 直到龙头砍完, 或者骑士用完。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define M(a) memset(a, 0, sizeof(a))
using namespace std; 
const int maxn = 20000 + 10;
int head[maxn], knight[maxn];
int main() {
	int n, m;
	while(scanf("%d%d", &n, &m) == 2 && n && m) {
		long long ans = 0;
		M(head); M(knight);
		int phead = 0, pknight = 0;
		for(int i = 0; i < n; i++) scanf("%d", &head[i]);
		for(int i = 0; i < m; i++) scanf("%d", &knight[i]);
		sort(head, head + n); sort(knight, knight + m);
		while(phead < n && pknight < m) {
			if(head[phead] <= knight[pknight]) {
				ans += knight[pknight++];
				phead++;
			}
			else pknight++;
		}
		if(phead < n) printf("Loowater is doomed!\n");
		else printf("%lld\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39645344/article/details/83046413