D. Too Easy Problems
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly ti milliseconds or ignore it and spend no time. You don’t need time to rest after solving a problem, either.
Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer ai to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than ai problems overall (including problem i).
Formally, suppose you solve problems p1, p2, …, pk during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≤ apj.
You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don’t forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do.
Input
The first line contains two integers n and T (1 ≤ n ≤ 2·105; 1 ≤ T ≤ 109) — the number of problems in the exam and the length of the exam in milliseconds, respectively.
Each of the next n lines contains two integers ai and ti (1 ≤ ai ≤ n; 1 ≤ ti ≤ 104). The problems are numbered from 1 to n.
Output
In the first line, output a single integer s — your maximum possible final score.
In the second line, output a single integer k (0 ≤ k ≤ n) — the number of problems you should solve.
In the third line, output k distinct integers p1, p2, …, pk (1 ≤ pi ≤ n) — the indexes of problems you should solve, in any order.
If there are several optimal sets of problems, you may output any of them.
Examples
Input
5 300
3 100
4 150
4 80
2 90
2 300
Output
2
3
3 1 4
Input
2 100
1 787
2 788
Output
0
0
Input
2 100
2 42
2 58
Output
2
2
1 2
Note
In the first example, you should solve problems 3, 1, and 4. In this case you’ll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won’t. You’ll score two points.
In the second example, the length of the exam is catastrophically not enough to solve even a single problem.
In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
问题分析
题意:要求在T时间内尽可能做更多的题,但是每道题都有限制:
1. 当前做的题量总数不能大于这道题的ai的值;
2. 当前所剩余总时间可以足够来做完这道题。
解题思路:想要尽可能的多做题,就必须要在两个限制内去尽可能去做用时少,ai的值大的题。所以做题之前就需要排一下序。如果ai相同则选择用时少的,不相同就先选择ai较大的。
bool cmp(ques a,ques b)
{
if(a.v==b.v)
return a.t<b.t;
else
return a.v>b.v;
}
然后等利用优先队列的特性,用后来用时较少的题来替换堆顶的。
if(!q.empty()&&a[i].t<q.top().t) //要注意队列不能为空
{
t = t + q.top().t - a[i].t; //总时间t加上出队的题用时再减去入队的题的用时
q.pop();
q.push(a[i]);
}
这样用贪心的思想外加优先队列的辅助即可完成尽可能的做更多的题。
好,下面上AC code(^_^)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int N = 2e5+3;
int n,t;
struct ques
{
int t,i,v;
friend bool operator <(ques a,ques b)
{
return a.t<b.t;
}
}a[N];
bool cmp(ques a,ques b)
{
if(a.v==b.v)
return a.t<b.t;
else
return a.v>b.v;
}
int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&t))
{
priority_queue<ques> q;
for(int i = 1; i <= n; i++)
{
scanf("%d%d",&a[i].v,&a[i].t);
a[i].i = i;
}
sort(a+1,a+n+1,cmp);
ques que;
for(int i = 1; i <= n; i++)
{
if(q.size()>=(unsigned)a[i].v) break;
if(a[i].t<=t)
{
q.push(a[i]);
t -= a[i].t;
}else if(!q.empty()&&a[i].t<q.top().t)
{
t = t + q.top().t - a[i].t;
q.pop();
q.push(a[i]);
}
}
int k = q.size();
printf("%d\n%d\n",k,k);
while(!q.empty())
{
que = q.top();
q.pop();
printf("%d%c",que.i,(q.size()==0)?'\n':' ');
}
}
return 0;
}