uva - 11292 - Dragon of Loowater

题意:n条龙,m个骑士,n条龙的头的半径,m个骑士力所能及砍龙头半径,每一厘米,需支付骑士1个coin,问最少需支付多少个coin才能slay所有的龙,无解时,输出Loowater is doomed!。

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=2267

——>>先把龙的半径从小到大排序,骑士的能力从小到大排序,然后对骑士数组扫描一次即可。

#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 20000 + 10;

int main()
{
    int n, m, dragon[maxn], knight[maxn];
    while(cin>>n>>m)
    {
        if(n == 0 && m == 0) return 0;
        int i;
        for(i = 0; i < n; i++) cin>>dragon[i];
        for(i = 0; i < m; i++) cin>>knight[i];
        sort(dragon, dragon+n);     //先排序
        sort(knight, knight+m);
        int cur = 0, cost = 0;      //cur为目前恶龙的下标,从0开始,cost为所需总资金
        for(i = 0; i < m; i++)
        {
            if(knight[i] >= dragon[cur])
            {
                cost += knight[i];
                if(++cur == n) break;
            }
        }
        if(cur == n) cout<<cost<<endl;
        else cout<<"Loowater is doomed!"<<endl;
    }
    return 0;
}


posted @ 2013-01-14 11:04  xiaodanding  阅读(118)  评论(0编辑  收藏  举报