uva - 11292 - Dragon of Loowater(贪心)

题意:(搬自lrj大大的书)你的国王里边有一条n个头的恶龙,你希望雇佣一些骑士把它杀死,(砍掉所有的头)。村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币。要砍掉所有的头,且支付金币最少。

方法:对头的直径和骑士的能力值排序,比较即可。贪心算法。

#include <iostream>  
#include <iomanip>  
#include <string>  
#include <cstring>  
#include <cstdio>  
#include <queue>  
#include <stack>  
#include <algorithm>  
#include <cmath>  

using namespace std;

#define MAX 20000+10
int head[MAX], warrior[MAX], n, m, sum;

void Input()
{
	int i = 0, j = 0;
	for (i = 0; i < n; i++)
		cin >> head[i];
	for (i = 0; i < m; i++)
		cin >> warrior[i];
}

void Calulate()
{
	int i = 0, j = 0;
	while (j < m)
	{
		if (warrior[j] >= head[i])
		{
			sum += warrior[j], i++, j++;
		}
		else
		{
			j++;
		}
		if (i == n)
		{
			cout << sum << endl;
			return;
		}
	}
	cout << "Loowater is doomed!" << endl;
}

int main()
{
#ifdef Local  
	freopen("a.in", "r", stdin);  
#endif  
	int i = 0, j = 0;
	while (cin >> n >> m && n+m)
	{
		memset(head, 0, sizeof(head));
		memset(warrior, 0, sizeof(warrior));
		sum = 0;
		Input();
		sort(head, head+n);
		sort(warrior, warrior+m);
		Calulate();
	}
}

提交的情况已经无力吐槽了,手术完了真心没精神做题(借口)。。错误原因是sum累加时候warrior[j]写成了i。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值