第一章例题1勇者斗蛟龙UVa 11292(贪心)
2014-07-25 13:56
218 查看
11292 - Dragon of Loowater
Time limit: 1.000 secondsProblem C: The Dragon of Loowater
Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack
one of the people, and perhaps bite off a finger or two, but in general, the people tolerated the geese as a minor nuisance.
One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called on his
knights to slay the dragon and save the kingdom.
The knights explained: "To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon's heads. The heads of the dragon are of different sizes. In order to chop 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 paid a wage equal to one gold coin for each centimetre of the knight's height."
Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted 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, you and the whole kingdom would be burnt to a crisp!
Input Specification:
The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the numbern of heads that the dragon has, and the number mof knights in the kingdom. The next
n lines each contain an integer, and give the diameters of the dragon's heads, in centimetres. The following
m lines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.
The last test case is followed by a line containing:
0 0
Output Specification:
For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:Loowater is doomed!
Sample Input:
2 3
5
4
7
8
4
2 1
5
5
10
0 0
Output for Sample Input:
11
Loowater is doomed!
/*就是贪心,将两个数组排序*/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=20000+10;
int a[maxn],b[maxn];//a和b分别用来存储龙的头和骑士的实力值
int n,m;
int main()
{
while(cin>>n>>m)
{
if(n==0&&m==0){break;}
int i;
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
for(i=0;i<m;i++)
{scanf("%d",&b[i]);}
sort(a,a+n);
sort(b,b+m);
/*以骑士为标准,从能力最小的骑士开始试探,
如果所有的骑士杀不掉n个龙头则j的值达不到n,
如果骑士可以杀掉n个龙头,则j可以达到n,此时要及时break*/
int j=0,cost=0;//用j作为龙头的下标,用i作为骑士的下标
for(i=0;i<m;i++)
{
if(b[i]>=a[j])
{
cost+=b[i];
j++;
if(j==n){break;}//当j等于n时表示所有的龙头都被砍下要退出,否则可能访问非法内存
}
}
if(j==n){cout<<cost<<endl;}
else {cout<<"Loowater is doomed!"<<endl;}
}
return 0;
}
相关文章推荐
- 指南第一章 例题一 UVA 11292 Dragon of Loowater(贪心算法)
- 第一章例题2突击战UVa11729(贪心)
- UVa-11292 Dragon of Loowater (贪心)
- UVaThe Dragon of Loowater-11292(排序+贪心)
- Uva---11292Problem C: The Dragon of Loowater (贪心)
- uva11292 The Dragon of Loowater (排序+贪心)
- UVa 11292 The Dragon of Loowater 【贪心】
- UVA.11292 Dragon of Loowater (贪心)
- uva11292 The Dragon of Loowater (排序+贪心)
- 第一章例题10正整数序列UVa 11384(找规律)
- uva 11292 - Dragon of Loowater(贪心)
- UVa11292 - Dragon of Loowater (贪心+模拟)
- 第一章例题3分金币UVa 11300(中位数)
- UVa(11292),贪心水题
- UVa - 11292 - Dragon of Loowater ( 排序 + 贪心 )
- 例题1.8 彩色立方体 Colored Cubes UVALive - 3401 暴力打表+暴力搜索+贪心
- UVa 11292 - Dragon of Loowater(排序贪心)
- UVA 11292 Dragon of Loowater(简单贪心)
- 第一章例题7偶数矩阵UVa 11464(枚举)
- [训练指南] 第一章 例题3 分金币 (Spreading the wealth,Uva 11300)