cogs 1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292]

1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292]

★   输入文件:DragonUVa.in   输出文件:DragonUVa.out   简单对比
时间限制:1 s   内存限制:256 MB

【题目描述】

King(CH)的王国里有一条n个头的恶龙,他希望雇佣一些骑士把它杀死(即砍掉所有的头)。小酒馆里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉一个直径不超过x的头,且需要支付x个金币。如何雇用骑士才能砍掉所有恶龙的头,且需要支付的金币最少?注意:一个骑士只能砍一个头(且不能被雇佣2次)。

【输入格式】

输入包含多组数据。数据的第一行为正整数n和m(1≤n,m≤20 000);以下n行每行为一个整数,即恶龙每个头的直径;以下m行每行为一个整数,即每个骑士的能力。

【输出格式】

输出最小花费。如果无解,输出"Loowater is doomed"。(无解的情况为龙头砍不完)

【样例输入】

2 3
5
4
7
8
4

【样例输出】

11

【提示】

小酒馆里向来是可以寻得大量雇佣军的。

雇佣兵战斗属性较强,但属性固定,不能增强或升级。

雇主是不需要为雇佣兵的伤亡负责的。

【来源】

The Dragon of Loowater,UVa 11292,数据来自[Chen Hao,Algorithms Consult]

思路:显而易见是一个贪心

第一种贪心方法:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 20010
using namespace std;
int n,m,pos=1,ans;
int cost[MAXN],val[MAXN];
int main(){
    freopen("DragonUVa.in","r",stdin);
    freopen("DragonUVa.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)    scanf("%d",&val[i]);
    for(int i=1;i<=m;i++)    scanf("%d",&cost[i]);
    sort(val+1,val+1+n);
    sort(cost+1,cost+1+m);
    for(int i=1;i<=m;i++){
        if(pos>n)    break;
        if(cost[i]>=val[pos]){
            ans+=cost[i];
            pos++;
        }
    }
    if(pos-1==n)    cout<<ans;
    else cout<<"Loowater is doomed";
}

第二种贪心方法:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 20010
using namespace std;
int n,m,pos,ans;
int cost[MAXN],val[MAXN];
int main(){
    freopen("DragonUVa.in","r",stdin);
    freopen("DragonUVa.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)    scanf("%d",&val[i]);
    for(int i=1;i<=m;i++)    scanf("%d",&cost[i]);
    sort(val+1,val+1+n);
    sort(cost+1,cost+1+m);
    for(int i=1;i<=n;i++){
        while(cost[++pos]<val[i]){
            if(pos>m){
                cout<<"Loowater is doomed";
                return 0; 
            }
        }
        ans+=cost[pos];
    }
    cout<<ans;
}

 

posted @ 2017-10-24 16:15  一蓑烟雨任生平  阅读(168)  评论(0编辑  收藏  举报