1. 程式人生 > >UVA 11729

UVA 11729

進行 include sin 運算符 ret per 貪心 \n ace

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\n", 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\n", kase++, ans);
    }
    return 0;
}

UVA 11729