UVa 11729 Commando War 突击战

我们使用贪心算法,看看哪个是决定最大长度,从这个切入点入手。然后计算最大时间 

#include "iostream"
#include "vector"
#include "algorithm"
using namespace std;
struct Job {
    int b, j;
    bool operator < (const Job& x) const {
        return j > x.j;
    }
};
int main() {
    int N, b, j, cnt = 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){b, j});
        }
        sort(v.begin(), v.end());
        int talk_time = 0, do_time = 0;
        for(int i = 0; i < N; i++) {
            talk_time += v[i].b;
            do_time = max(do_time, talk_time + v[i].j);
        }
        printf("Case %d: %d\n", cnt++, do_time);
    }
    return 0;
}
// N 个不下
// B J
// B分钟交代执行J分钟任务
/*
3 
2 5
3 2
2 1
3
3 3
4 4
5 5
0

*/

猜你喜欢

转载自www.cnblogs.com/littlepage/p/12718816.html
war