算法竞赛入门经典 勇者斗恶龙

/*
	Name: 勇者斗恶龙 
	Copyright: 刘汝佳 
	Author: Analyst
	Date: 27/02/14 23:20
	Description: dev-cpp 5.5.3
*/
#include <iostream>
#include <algorithm>   //用到sort 
using namespace std;

const int maxn = 2000+5; 
int A[maxn], B[maxn];
int main()
{
	int n, m;
	
	while (scanf("%d%d",&n, &m) == 2 && n && m)
	{
		for (int i = 0; i < n; ++i)
			scanf("%d", &A[i]);
		for (int i = 0; i < m; ++i)
			scanf("%d", &B[i]);	
		sort(A, A+n);
		sort(B, B+m);
		
		int cur = 0;    //当前需砍掉的头编号 
		int cost = 0;   //当前总费用 
		
		for (int i = 0; i < m; ++i)
			if (B[i] >= A[cur])
			{
				cost += B[i];     //雇佣该骑士 
				if (++cur == n)   //如果头砍完,退出 
					break;
			}
		if (cur < n)
			printf("Loowater is doomed!\n");
		else
			printf("%d\n",cost);
	}
	return 0;
}

ps: 

scanf()  返回值:

scanf("%d%d",&m,&n);    >> 2 3   返回2

scanf("%d%d",&m,&n);    >> 2 a   返回1

sort() 从小到大排序函数。

你可能感兴趣的:(算法竞赛入门经典 勇者斗恶龙)