思路:二分check k
代码:
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int N=2e5+5; struct pro{ int t,a,id; bool operator < (pro tmp){ return t<tmp.t; } }a[N]; int n,t; bool check(int k){ int cnt=0; int s=0; for(int i=1;i<=n;i++){ if(a[i].a>=k){ if(s+a[i].t<=t)cnt++,s+=a[i].t; else break; } } if(cnt>=k)return true; else return false; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>t; for(int i=1;i<=n;i++){ cin>>a[i].a>>a[i].t; a[i].id=i; } sort(a+1,a+1+n); int l=0,r=n,m=(l+r)>>1; while(l<r){ //cout<<m<<' '<<l<<' '<<r<<endl; if(check(m))l=m; else r=m-1; m=(l+r+1)>>1; } cout<<m<<endl; cout<<m<<endl; int s=0,cnt=0; for(int i=1;i<=n;i++){ if(a[i].a>=m){ if(s+a[i].t<=t)cout<<a[i].id<<" ",cnt++,s+=a[i].t; else break; } if(cnt==m)break; } cout<<endl; return 0; }