BZOJ 3709 Bohater 【贪心】

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3709

题解:
对于杀完能加血的怪物,直接按照消耗从小到大排序,否则将血量从大到小排,z 需要用 long long !100000^2 会爆int!

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map> // STL
#include <string> 
#include <vector>
#include <queue>
#include <stack>
#define mpr make_pair
#define debug() puts("okkkkkkkk")

using namespace std;

typedef long long LL;

const int inf = 1 << 26;

struct nodes {
    int d, a, id;
} a[200005], b[200005];

int n, cnt1 = 0, cnt2 = 0;
LL z;

int cmp(nodes a, nodes b) {
    return a.d < b.d;
}

int cmp2(nodes a, nodes b) {
    return a.a > b.a;
}

int main(){
    scanf("%d %d", &n, &z);
    for ( int i = 1; i <= n; i ++ ) {
        int x, y;
        scanf("%d %d", &x, &y);
        if(x <= y) {
            a[++ cnt1].d = x;
            a[cnt1].a = y;
            a[cnt1].id = i;
        } else {
            b[++ cnt2].d = x;
            b[cnt2].a = y;
            b[cnt2].id = i;
        }
    }

    sort(a+1, a+cnt1+1, cmp);

    for ( int i = 1; i <= cnt1; i ++ ) {
        if(z <= a[i].d) { puts("NIE"); return 0; }
        z = z-a[i].d+a[i].a;
    }

    sort(b+1, b+cnt2+1, cmp2);
    for ( int i = 1; i <= cnt2; i ++ ) {
        if(z <= b[i].d) { puts("NIE"); return 0; }
        z = z-b[i].d+b[i].a;
    }

    puts("TAK");
    for ( int i = 1; i <= cnt1; i ++ ) printf("%d ", a[i].id);
    for ( int i = 1; i <= cnt2; i ++ ) printf("%d ", b[i].id);
    puts("");

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34896694/article/details/78090104