链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1029
思路: 按结束时间排序,优先选结束时间短的,选完后扔到优先队列里(大的优先),如果选到某个点不能在规定时间内完成,我们就将优先队列的队首元素与当前点所需时间比较下,如果队首元素所需时间大于当前点,那么就不选择队首元素,选择当前点。
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 2e5+;
struct node{
int x,y;
}a[M];
bool cmp(node a,node b){
if(a.y == b.y) return a.x < b.x;
return a.y < b.y;
}
priority_queue<int>q;
int main(){
int n;
cin>>n;
for(int i = ;i <= n;i ++){
cin>>a[i].x>>a[i].y;
}
sort(a+,a++n,cmp);
int now = ,ans = ;
for(int i = ;i <= n;i ++){
if(now + a[i].x <= a[i].y){
ans++;q.push(a[i].x);now += a[i].x;
}
else{
int tp = q.top();
if(tp > a[i].x){
q.pop();q.push(a[i].x); now -= tp-a[i].x;
}
}
}
cout<<ans<<endl;
}