UVA 11729

Posted ronnielee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 11729相关的知识,希望对你有一定的参考价值。

UVA 11729

大体是贪心做法,执行时间长的先交代

训练指南原题解

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

struct Job 
{
    int j, b;
    bool operator < (const Job& x) const 
    {
        return j > x.j;
    }
};

int main() 
{
    int n, b, j, kase = 1;
    while(scanf("%d", &n) == 1 && n) 
    {
        vector<Job> v;
        for(int i = 0; i < n; i++) 
        {
            scanf("%d%d", &b, &j); 
            v.push_back((Job){j,b});
        }
        sort(v.begin(), v.end());

        int s = 0;
        int ans = 0;

        for(int i = 0; i < n; i++)
        {
            s += v[i].b;
            ans = max(ans, s+v[i].j);
        }
        printf("Case %d: %d
", kase++, ans);
    }
    return 0;
}

用cmp函数进行排序而非重载运算符

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

struct Job 
{
    int j, b;
    bool operator < (const Job& x) const 
    {
        return j > x.j;
    }
};

int main() 
{
    int n, b, j, kase = 1;
    while(scanf("%d", &n) == 1 && n) 
    {
        vector<Job> v;
        for(int i = 0; i < n; i++) 
        {
            scanf("%d%d", &b, &j); 
            v.push_back((Job){j,b});
        }
        sort(v.begin(), v.end());

        int s = 0;
        int ans = 0;

        for(int i = 0; i < n; i++)
        {
            s += v[i].b;
            ans = max(ans, s+v[i].j);
        }
        printf("Case %d: %d
", kase++, ans);
    }
    return 0;
}

以上是关于UVA 11729的主要内容,如果未能解决你的问题,请参考以下文章

Uva11729 Commando War

UVA 11729 Commando War

经典贪心算法uva11729

UVA 11729 Commando War (贪心)

UVA 11729 Commando War

UVa11729 - Commando War