①以时间排序,选择耗时最少的题目
②在①的基础上用优先队列维护答案,对于不满足老师要求的题目(a i < ans )我们剔除其中花费时间最多的,到能再次满足要求为止。③在①和②的基础上,如果剩下的时间不足以完成接下来哪怕一道题了,就不必再作选择,这就是最优解。
注意贪心的顺序。
代码:
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct prob{
int least,sec,id;
}a[200005];
bool cmp(prob x,prob y){
return x.sec<y.sec;
}
struct comp{
bool operator()(const prob &x,const prob &y){
if(x.least!=y.least)return x.least>y.least;return x.sec<y.sec;
}
};
priority_queue<prob,vector<prob>,comp> an;
int main(){
int n,ans=0,top=0,t;
scanf("%d%d",&n,&t);
for(int i=0;i<n;i++){
scanf("%d%d",&a[i].least,&a[i].sec);
a[i].id=i+1;
}
sort(a,a+n,cmp);
for(int i=0;i<n;i++){
if(t<a[i].sec)break;//策略③
if(a[i].least>ans){//策略①
t-=a[i].sec;
an.push(a[i]);
ans++;
//printf("%d",ans);
}
while(!an.empty()&&an.top().least<ans){//策略②
ans--;
t+=an.top().sec;
an.pop();
}
}
printf("%d\n%d\n",ans,ans);
while(!an.empty())printf("%d ",an.top().id),an.pop();
}