bzoj 3709 [PA2014]Bohater 貪心
阿新 • • 發佈:2018-08-15
operator while type pa2014 include 時間復雜度 pro ++ https
題面
題目傳送門
解法
顯然是貪心題吧,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\n"; 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\n"; return 0;} else m += b[i].y - b[i].x, ans[++len] = b[i].id; cout << "TAK\n"; for (int i = 1; i <= n; i++) cout << ans[i] << ‘ ‘; cout << "\n"; return 0; }
bzoj 3709 [PA2014]Bohater 貪心