题解:
贪心
每一次和堆中最大的进行交换
代码:
#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; }