bzoj1029

堆+贪心(优先队列)

我们先按t2升序排一遍,对于每栋建筑如果可以抢修就修,如果不能就看它是否比之前最大的更优,如果更优,就替换。

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=150000+10;
struct hh
{
  LL a,b;
}e[maxn];
int n;
LL ans,sum;
priority_queue<LL>q;
template <class T> void read(T&x)
{
  x=0;char c=getchar();int f=0;
  while(c<'0'||c>'9'){f|=(c=='-');c=getchar();}
  while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
  x=f?-x:x;
}
bool cmp(const hh&a,const hh&b){return a.b<b.b;}
int main()
{
  read(n);
  for(register int i=1;i<=n;i++){read(e[i].a);read(e[i].b);}
  sort(e+1,e+1+n,cmp);
  for(register int i=1;i<=n;i++)
  {
      if(sum+e[i].a<e[i].b)sum+=e[i].a,ans++,q.push(e[i].a);
      else if(e[i].a<q.top())
      {
        sum-=q.top();q.pop();
        sum+=e[i].a;q.push(e[i].a);
    }
  }
  printf("%lld",ans);
  return 0;
}
View Code

 

posted @ 2017-10-31 09:41  新手-周  阅读(204)  评论(0编辑  收藏  举报