bzoj 1029: [JSOI2007]建筑抢修

Description

  小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的
入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
的建筑。

解题报告:
正解:贪心
因为此题时间和N较大不能dp,所以考虑贪心,首先按报废时间排序是显然的,然后考虑修和不修的问题,如果能修就修,但是显然是不对的,需要用堆调整贪心,如果当前的建筑已经不能拯救,那么我们考虑用他来替换掉之前的修建时间所需比他大的建筑,这样答案不变,总时间却减少了,所以更优

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=150005;
struct comp{bool operator()(ll i,ll j)const{return i<j;}};
priority_queue<ll,vector<ll>,comp>q;
int n;
struct node{
	ll x,y;
	bool operator<(const node &pr)const{return y<pr.y;}
}a[N];
void work()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i].x,&a[i].y);
	sort(a+1,a+n+1);
	int ans=0;ll tot=0;
	for(int i=1;i<=n;i++){
		if(tot>a[i].y)continue;
		if(tot+a[i].x<=a[i].y){
			ans++;q.push(a[i].x);tot+=a[i].x;
			continue;
		}
		if(!q.empty() && q.top()>a[i].x){
			tot-=q.top();tot+=a[i].x;
			q.pop();q.push(a[i].x);
		}
	}
	printf("%d
",ans);
}

int main()
{
	work();
	return 0;
}

原文地址:https://www.cnblogs.com/Yuzao/p/7599357.html