The Dragon of Loowater_uva11292_poj3646

    排序+贪心

    注意边界的处理。

    我是用龙的头数进行for循环,依次对骑士进行选择。假如符合条件的骑士都用完了,而龙头还没有砍完,则不成功。

    32ms

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int _max=20000+10;
 5 int a[_max];
 6 int b[_max];
 7 int main()
 8 {
 9     int n,m;
10     while(scanf("%d%d",&n,&m)!=EOF){
11         if(n==0&&m==0)
12             break;
13         for(int i=0;i<n;i++)
14             scanf("%d",&a[i]);
15         for(int i=0;i<m;i++)
16             scanf("%d",&b[i]);
17         sort(a,a+n);
18         sort(b,b+m);
19         if(m<n||a[n-1]>b[m-1])
20             printf("Loowater is doomed!\n");
21         else{
22             int ans=0;
23             int v=-1;
24             for(int i=0;i<n;i++){
25                 v++;
26                 while(b[v]<a[i]&&v<m){
27                     v++;
28                 }
29                 if(v>=m){
30                     ans=-1;
31                     break;
32                 }
33                 ans+=b[v];
34             }
35             if(ans>=0)
36                 printf("%d\n",ans);
37             else
38                 printf("Loowater is doomed!\n");
39         }
40     }
41     return 0;
42 }

    而刘汝佳的训练指南里,是用骑士的人数进行for循环,依次砍掉龙头,要是最后龙头数>=n,则说明砍完了,成功。

    他的代码写的比我漂亮多了。

    47ms

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int maxn=20000+5;
 5 int a[maxn];
 6 int b[maxn];
 7 int main()
 8 {
 9     int n,m;
10     while(scanf("%d%d",&n,&m)==2&&n&&m){
11         for(int i=0;i<n;i++)
12             scanf("%d",&a[i]);
13         for(int i=0;i<m;i++)
14             scanf("%d",&b[i]);
15         sort(a,a+n);
16         sort(b,b+m);
17         int cur=0;
18         int cost=0;
19         for(int i=0;i<m;i++){
20             if(b[i]>=a[cur]){
21                 cost+=b[i];
22                 if(++cur==n)
23                     break;
24             }
25         }
26         if(cur<n)
27             printf("Loowater is doomed!\n");
28         else
29             printf("%d\n",cost);
30     }
31     return 0;
32 }

时间: 2024-10-20 04:57:40

The Dragon of Loowater_uva11292_poj3646的相关文章

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

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

HDU 4362 Dragon Ball 贪心DP

Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball wil

[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行 骑士的能力值 输出: 如果能砍完所有

HDU 3635 Dragon Balls (并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3360    Accepted Submission(s): 1303 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

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

UVA11292 HDU1902 POJ3646 Dragon of Loowater

问题链接:UVA11292 HDU1902 POJ3646 Dragon of Loowater. 这个问题是一个典型的贪心法问题,求代价最小. 由于需要用到排序函数,C++的排序函数参数比较简单,所以用C++编程. AC通过的C++语言程序如下: /* UVA11292 HDU1902 POJ3646 Dragon of Loowater */ #include <cstdio> #include <algorithm> using namespace std; #define

龙书(Dragon book) +鲸书(Whale book)+虎书(Tiger book)

1.龙书(Dragon book)书名是Compilers: Principles,Techniques,and Tools作者是:Alfred V.Aho,Ravi Sethi,Jeffrey D.Ullman国内所有的编译原理教材都是抄的它的,而且只是抄了最简单的前端的一些内容.龙书中文版第一版龙书英文版第二版 2.鲸书(Whale book)书名是:Advanced Compiler Design and Implementation作者是:Steven S.Muchnick也就是高级编译

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 &&