如何严谨证明此题的反悔贪心?
查看原帖
如何严谨证明此题的反悔贪心?
494699
卷王慢即快楼主2024/2/24 15:00

贪心代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
#define up(i, l, r) for(int i = (l); i <= (r); i++)
#define down(i, r, l) for(int i = (r); i >= (l); i--)
const int mod = 1000000007;
const int base = 2333;
const double eps = 1e-6;

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
	return x * f;
}

int n, m, k, Q, T, _, ans = 0;

struct node {
	int t, d;
} a[200007];
priority_queue<ll> q;

inline bool cmp(node x, node y) {
	return x.d < y.d;
}

signed main() {
	n = read();
	up(i, 1, n) a[i].t = read(), a[i].d = read();
	sort(a + 1, a + n + 1, cmp);
	ll sum = 0;
	up(i, 1, n) {
		q.push(a[i].t); sum += a[i].t;
		if(sum > a[i].d) {
			sum -= q.top();
			q.pop();
		}
		else ans++;
	}
	printf("%d", ans);
	return 0;
}

求完整证明。

2024/2/24 15:00
加载中...