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 numberm of knights in the kingdom. The nextn lines each contain an integer, and give the diameters of the dragon's heads, in centimetres. The followingmlines 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 3
5
4
7
8
4
2 1
5
5
10
0 0

Output for Sample Input:

11
Loowater is doomed!

 题目大致意思是能力比龙头大的骑士可以完成任务,每个骑士要等同于能力的金币,我们要找出如何可以使用最少的金币来杀死龙,如果不能杀死龙就输出
Loowater is doomed!
由此我们可以把问题划分成两块:
一、能不能杀死龙
这里我们只需要判断龙不能被杀死的情况就可以了。根据题目我们可以得出
1.当龙头的数量多于骑士的数量时龙不能被杀死。
2.当能杀死龙的骑士数目少于龙头数时。
(没有能杀死龙的骑士包含在了第二种情况中)
二、如何使用最少金币,即贪心的部分
理解这两个部分后就可以写出代码:
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    long long n,m;
    while(cin >> n >> m && n != 0 || m != 0){
        int dra[n + 1],kin[m + 1];
        for(int i = 0;i < n;++i)
            cin >> dra[i];
        for(int i = 0;i < m;++i)
            cin >> kin[i];
        sort(dra,dra + n);
        sort(kin,kin + m);
        if(n > m){
            cout << "Loowater is doomed!" << endl;
        }
        else{
            int j = 0;
            int res = 0;
            for(int i = 0;i < m;i++){
                if(dra[j] <= kin[i]){
                    res += kin[i];
                    if(++j == n)
                        break;
                }
            }
            if(j >= n)
            cout << res << endl;
            else
            cout << "Loowater is doomed!" << endl;
        }
    }
    return 0;
}
再我第一次做的时候写出了这样一个代码:
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    long long n,m;
    while(cin >> n >> m && n != 0 || m != 0){
        int dra[n + 1],kin[m + 1];
        for(int i = 0;i < n;++i)
            cin >> dra[i];
        for(int i = 0;i < m;++i)
            cin >> kin[i];
        sort(dra,dra + n);
        sort(kin,kin + m);
        long long mink;
        bool did;
        long long res = 0;
        for(int i = 0;i < n;++i){
            did = false;
            for(int j = 0;j < m;++j){
                if(dra[i] <= kin[j]){
                    mink = kin[j];
                    kin[j] = 0;
                    did = true;
                    break;
                }
            }
            if(did == false)
                break;
            res += mink;
        }
        if(did)
            cout << res << endl;
        else
            cout << "Loowater is doomed!" << endl;
    }
    return 0;

}
我认为并没有出现什么问题,但是oj时WA。之后我将kin【j】 = 0,改为了记下坐标每次后移就成功ac了
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    long long n,m;
    while(cin >> n >> m ){
        if(n == 0 && m == 0)
            break;
        int dra[20001] = {},kin[20001] = {};
        for(int i = 0;i < n;++i)
            cin >> dra[i];
        for(int i = 0;i < m;++i)
            cin >> kin[i];
        sort(dra,dra + n);
        sort(kin,kin + m);
        bool did;
        int an = -1;
        long long res = 0;
        for(int i = 0;i < n;++i){
            did = false;
            for(int j = an+1;j < m;++j){
                if(dra[i] <= kin[j]){
                    res += kin[j];
                    an = j;
                    did = true;
                    break;
                }
            }
            if(did == false)
                break;
        }
        if(did)
            cout << res << endl;
        else
            cout << "Loowater is doomed!" << endl;
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/happy_fakelove/article/details/79018514
今日推荐