首页 > 其他 > 详细

UVA 11729

时间:2018-08-18 10:20:15      阅读:191      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/ronnielee/p/9495967.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!