题解:

贪心

每一次和堆中最大的进行交换

代码:

#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,less<int> > q;
int n,now,ans;
struct data{int t,ed;}a[150005];
int operator<(data a,data b)
{
    return a.ed<b.ed;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)scanf("%d%d",&a[i].t,&a[i].ed);
    sort(a+1,a+n+1);
    for (int i=1;i<=n;i++)
     if (now+a[i].t<=a[i].ed)
      {
        ans++;
        now+=a[i].t;
        q.push(a[i].t);
      }
     else 
      {
        int tmp=q.top();
        if (a[i].t<tmp)
         {
            q.pop();
            q.push(a[i].t);
            now=now-tmp+a[i].t;
         }
      }
    printf("%d",ans);
    return 0;
}

 

posted on 2018-03-14 21:01  宣毅鸣  阅读(114)  评论(0编辑  收藏  举报