Forever Young

洛谷 P4053 [JSOI2007]建筑抢修

代码

/*
By:Loceaner
*/
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

inline int read() {
	char c = getchar();
	int x = 0, f = 1;
	for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
	for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
	return x * f;
}

const int N = 2e5 + 11;

struct node{
	int t, r;
} d[N];

bool cmp(node a, node b) {
	return a.r != b.r ? a.r < b.r : a.t < b.t;
} 
 
int n, cnt, now;
priority_queue <int> Q; 

int main() {
	n = read();
	for(int i = 1; i <= n; i++) d[i].t = read(), d[i].r = read();
	stable_sort(d + 1, d + 1 + n, cmp);
	for(int i = 1; i <= n; i++) {
		if(now + d[i].t <= d[i].r) {
			now += d[i].t, cnt++, Q.push(d[i].t);
			continue;
		}
		if(Q.empty() || Q.top() <= d[i].t) continue;
		now -= Q.top(),now += d[i].t;
		Q.pop(), Q.push(d[i].t);
	}
	cout << cnt << '\n';
	return 0;
}
posted @ 2019-10-08 19:34  Loceaner  阅读(103)  评论(0编辑  收藏  举报