bzoj 3709 [PA2014]Bohater 贪心

Posted copperoxide

tags:

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

题面

题目传送门

解法

显然是贪心题吧,dp感觉并不能做

如果某一个怪可以回血,那么一定先打伤害低的

对于不能回血的怪,先打恢复血量高的

我们考虑已经知道了最后的血量,然后反过来考虑,一定是尽量加比较小的,才能使最后的血量尽量大

那么就变成每一次尽量取剩下的怪中回血量最大的那个

模拟一遍即可

时间复杂度:(O(n log n))

代码

#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - ‘0‘, c = getchar(); x *= f;
}
struct Node {
    int x, y, num, id;
    bool operator < (const Node &a) const {
        return num < a.num;
    }
} a[N], b[N];
int ans[N];
main() {
    int n, m, l1 = 0, l2 = 0;
    read(n), read(m);
    for (int i = 1; i <= n; i++) {
        int x, y; read(x), read(y);
        if (x <= y) a[++l1] = (Node) {x, y, x, i};
            else b[++l2] = (Node) {x, y, -y, i};
    }
    sort(a + 1, a + l1 + 1), sort(b + 1, b + l2 + 1);
    int len = 0;
    for (int i = 1; i <= l1; i++)
        if (m <= a[i].x) {cout << "NIE
"; return 0;}
            else m += a[i].y - a[i].x, ans[++len] = a[i].id;
    for (int i = 1; i <= l2; i++)
        if (m <= b[i].x) {cout << "NIE
"; return 0;}
            else m += b[i].y - b[i].x, ans[++len] = b[i].id;
    cout << "TAK
";
    for (int i = 1; i <= n; i++) cout << ans[i] << ‘ ‘;
    cout << "
";
    return 0;
}

以上是关于bzoj 3709 [PA2014]Bohater 贪心的主要内容,如果未能解决你的问题,请参考以下文章

[bzoj3709] [PA2014]Bohater

bzoj3709 [PA2014]Bohater

bzoj3709: [PA2014]Bohater(贪心)

bzoj 3709 [PA2014]Bohater 贪心

[PA2014]Bohater

题解P4025 [PA2014]Bohater