[UVA-11292] Dragon of Loowater(勇者斗恶龙) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接:(vjudge)https://vjudge.net/problem/UVA-11292

Uva上找了半天愣是没找到Problem在哪...vjudge大法好。

题目大意:

有n条龙和m个骑士,每条龙有一个邪恶值z,每个骑士有一个能力值x。如果骑士的能力值大于龙的邪恶值,骑士可以杀死龙,但同时需要支付x枚金币。

给出龙和骑士的数据,问要杀死所有的龙,最少需要支出多少金币。每个骑士只能雇用一次。

输入包含多组数据,以 0 0 作为输入全部结束的标志。

如果无法杀死所有的龙,输出Loowater is doomed!

分析:

给龙和骑士按照邪恶值/能力值从小到大进行排序,然后尽量用能力值小的骑士杀龙。

AC代码:

 1 #include<algorithm>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5
 6 const int MAXN = 20005;
 7
 8 inline void read(int &x)
 9 {
10     char ch = getchar(),c = ch;x = 0;
11     while(ch < ‘0‘ || ch > ‘9‘) c = ch,ch = getchar();
12     while(ch <= ‘9‘ && ch >= ‘0‘) x = (x<<1)+(x<<3)+ch-‘0‘,ch = getchar();
13     if(c == ‘-‘) x = -x;
14 }
15
16 int knight[MAXN],dragon[MAXN];
17
18 int main()
19 {
20     int m,n,ans,i,j,cnt;
21     while(1)
22     {
23         read(n),read(m);
24         if(m == 0 && n == 0)
25             return 0;
26         ans = 0,cnt = 1;
27         memset(knight,0,sizeof(knight));
28         memset(dragon,0,sizeof(dragon));
29         for(i = 1;i <= n;++ i)
30             read(dragon[i]);
31         for(j = 1;j <= m;++ j)
32             read(knight[j]);
33         std::sort(knight+1,knight+1+m);
34         std::sort(dragon+1,dragon+1+n);
35         for(int i = 1;i <= m;++ i)
36         {
37             if(knight[i] >= dragon[cnt])
38                 ans += knight[i],cnt ++;
39             if(cnt > n)
40                 break;
41         }
42         if(cnt <= n)
43             printf("Loowater is doomed!\n");
44         else
45             printf("%d\n",ans);
46     }
47     return 0;
48 } 
时间: 2024-10-20 07:33:48

[UVA-11292] Dragon of Loowater(勇者斗恶龙) 题解的相关文章

[2016-03-14][UVA][11292][Dragon of Loowater]

时间:2016-03-14 19:50:12 星期一 题目编号:[2016-03-14][UVA][11292][Dragon of Loowater] 题目大意: 有m个骑士要砍n条龙,每个骑士能看掉龙的头颅当且仅当其实的能力值大于龙的头的直径,每个其实砍掉一条龙需要付出数值上等于能力值的代价,问m个骑士能否砍完所有龙,能则输出最小代价,否则输出"Loowater is doomed!" 输入: 多组数据 每组数据 n m n行 龙头的直径 m行 骑士的能力值 输出: 如果能砍完所有

UVa 11292 Dragon of Loowater

1 /*UVa 11292 Dragon of Loowater*/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 int n,m; 8 int main(){ 9 while(scanf("%d%d",&n,&m) && n!=0 &&

UVA 11292 Dragon of Loowater(简单贪心)

Problem 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 predato

[UVA] 11292 - Dragon of Loowater [贪心+队列]

Problem C: The Dragon of Loowater Time limit: 1.000 seconds 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.

UVa 11292 - 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 c

UVa 11292 Dragon of Loowater (水题,排序)

题意:有n个条龙,在雇佣勇士去杀,每个勇士能力值为x,只能杀死头的直径y小于或等于自己能力值的龙,只能被雇佣一次,并且你要给x赏金,求最少的赏金. 析:很简单么,很明显,能力值高的杀直径大的,低的杀直径小的.所以我们先对勇士能力值从小到大排序,然后对龙的直径从小到大排序, 然后扫一遍即可,如某个勇士杀不龙,就可以跳过,扫到最后,如果杀完了就结束,输出费用,否则就是杀不完. 代码如下: #include <iostream> #include <cstdio> #include &l

UVA之11292 Dragon of Loowater

Problem 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 predato

·UVa」 11292 - Dragon of Loowater( 贪心 )

Problem 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 predato

UVA它11292 - Dragon of Loowater

Problem 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 predato

UVA 题目11292 Dragon of Loowater

Problem 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 predato