The Dragon of Loowater
关键词:模拟
Description
Once upon a time, in the Kingdom ofLoowater, a minor nuisance turned into a major problem.
The shores of Rellau Creek in centralLoowater had always been a prime breeding ground for geese. Due to the lack ofpredators, the geese population was out of control. The people of Loowatermostly kept clear of the geese. Occasionally, a goose would attack one of thepeople, and perhaps bite off a finger or two, but in general, the peopletolerated the geese as a minor nuisance.
One day, a freak mutation occurred, and oneof the geese spawned a multi-headed fire-breathing dragon. When the dragon grewup, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had amajor problem. The king was alarmed, and called on his knights to slay thedragon and save the kingdom.
The knights explained: "To slay thedragon, we must chop off all its heads. Each knight can chop off one of thedragon's heads. The heads of the dragon are of different sizes. In order tochop off a head, a knight must be at least as tall as the diameter of the head.The knights' union demands that for chopping off a head, a knight must be paida wage equal to one gold coin for each centimetre of the knight's height."
Would there be enough knights to defeat thedragon? The king called on his advisors to help him decide how many and whichknights to hire. After having lost a lot of money building Mir Park, the kingwanted to minimize the expense of slaying the dragon. As one of the advisors,your job was to help the king. You took it very seriously: if you failed, youand the whole kingdom would be burnt to a crisp!
Input
The input contains several test cases. Thefirst line of each test case contains two integers between 1 and 20000inclusive, indicating the number n of heads that the dragon has, and the numberm of knights in the kingdom. The next n lines each contain an integer, and givethe diameters of the dragon's heads, in centimetres. The following mlines eachcontain an integer, and specify the heights of the knights of Loowater, also incentimetres.
The last test case is followed by a linecontaining:
0 0
Output
For each test case, output a linecontaining the minimum number of gold coins that the king needs to pay to slaythe dragon. If it is not possible for the knights of Loowater to slay thedragon, output the line:
Loowater is doomed!
SampleInput
2 3
5
4
7
8
4
2 1
5
5
10
0 0
SampleOutput
11
Loowater is doomed!
Analyze
大致题意:n条恶龙,m个勇士,用勇士来杀恶龙。一个勇士只能杀一个恶龙。而且勇士只能杀直径不超过自己能力值的恶龙。每个勇士需要支付能力值一样的金币。
问杀掉所有恶龙需要的最少金币。
Code
#include <cstdio>
#include <algorithm>
using namespace std;
int An[20010], Am[20010];
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF && (n&&m) )
{
for(int i=0; i<n; i++)
scanf("%d", &An[i]);
for(int j=0; j<m; j++)
scanf("%d", &Am[j]);
if(n > m)
{
printf("Loowater is doomed!\n");
continue;
}
sort(An, An+n);
sort(Am, Am+m);
int tmp = 0;
int ans = 0, flag=0;
for(int i=0; i<n; i++)
{
flag = 0;
for(int j=tmp; j<m; j++)
{
if(An[i] <= Am[j])
{
flag = 1;
ans += Am[j];
Am[j] = -1;
tmp = j;
break;
}
}
if(!flag)
break;
}
if(!flag)
printf("Loowater is doomed!\n");
else
printf("%d\n", ans);
}
return 0;
}