UVA 11292-Dragon of Loowater (思维)

来源:互联网 发布:扬州大学网络教学凭条 编辑:程序博客网 时间:2024/06/06 03:57

Description

Download as PDF

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 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 m of knights in the kingdom. The nextn lines each contain an integer, and give the diameters of the dragon's heads, in centimetres. The followingm 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 3547842 155100 0

Output for Sample Input:

11Loowater is doomed!

                                                                            

题意:给出怪兽的值,骑士的值,要求能杀完全部怪兽所用的骑士的最小值,如果无法杀完所有怪兽则输出

Loowater is doomed!
思路:使用二分,求出最小大于等于怪兽值的骑士值。

           其实已经将数按从小到大的顺序排列,来一个循环找到的第一个大于等于的数就是最下值了,比二分方便,代码中注释掉的部分。

CODE:

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;const int Max=20004;int n[Max],m[Max];int main(){    //freopen("in","r",stdin);    int N,M;    while(~scanf("%d%d",&N,&M),N,M){        for(int i=0;i<N;i++){            scanf("%d",&n[i]);        }        for(int i=0;i<M;i++){            scanf("%d",&m[i]);        }        sort(n,n+N);        sort(m,m+M);        int ans=0;        int i=0;        int j=0;        for(i=0;i<N;i++){            int t=lower_bound(m+j,m+M,n[i])-m;            if(t==M) break;            ans+=m[t];            j=t+1;        }        /*        int j=0;        for(int i=0;i<m;i++){            if(m[i]>=n[j]){                ans+=m[i];                if(++j==n) break;            }        }        */        if(ans==0 || i<N){            printf("Loowater is doomed!\n");        }        else printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击