[JSOI2007]建筑抢修(贪心+后悔)
吐槽
这是一道经典的贪心后悔的题目
做过贪心加后悔的题目的应该一眼可以看出来
解题思路
code(没注释QwQ)
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<ctime>
#include<queue>
#include<stack>
#include<vector>
#define rg register
#define il inline
#define lst long long
#define ldb long double
#define N 150050
using namespace std;
const int Inf=1e9;
int n,ans;
struct HOUSE{
int fixt,badt;
bool operator<(rg HOUSE a)
{
return badt<a.badt;
}
}ljl[N];
priority_queue<int>Q;
il int read()
{
rg int s=0,m=0;rg char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')m=1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return m?-s:s;
}
int main()
{
n=read();
for(rg int i=1;i<=n;++i)
ljl[i]=(HOUSE){read(),read()};
rg int now=0;
sort(ljl+1,ljl+n+1);
for(rg int i=1;i<=n;++i)
{
if(now+ljl[i].fixt>ljl[i].badt)
{
if(!Q.empty())
if(ljl[i].fixt<Q.top())
{
now-=Q.top()-ljl[i].fixt;
Q.pop();
Q.push(ljl[i].fixt);
}
}
else
{
now+=ljl[i].fixt;
Q.push(ljl[i].fixt);
}
}
while(!Q.empty())ans++,Q.pop();
printf("%d\n",ans);
return 0;
}