洛谷 P2949 [USACO09OPEN]Work Scheduling G

题目传送门

正难则反,根据时间从后往前倒着看,只要每到一个时间节点,能加任务就加任务,然后选一个受益最大的任务.(注意,时间0也可能有任务)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

int n,p;
long long ans;
struct kkk{
	int t,m;
}e[100001];
priority_queue<int> q;

inline bool cmp(kkk a,kkk b) {
	return a.t < b.t;
}

int main() {
	scanf("%d",&n);
	for(int i = 1;i <= n; i++)
		scanf("%d%d",&e[i].t,&e[i].m);
	sort(e+1,e+n+1,cmp);
	p = e[n].t;
	while(n > 0) {
		for(n;n >= 0; n--) {
			if(e[n].t == p) q.push(e[n].m);
			else  {
				p = e[n].t;
				break;
			}
		}
		for(int i = 1;i <= e[n+1].t - e[n].t; i++) {
			if(q.empty()) break;
			int u = q.top();
			q.pop();
			ans += u;
		}
	}
	printf("%lld",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/lipeiyi520/p/13886862.html