Too Easy Problems (Codeforces 913D)
Tags: Greedy, sortings

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.

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.

Sample Inputs:
5 300
3 100
4 150
4 80
2 90
2 300

2 100
1 787
2 788

2 100
2 42
2 58

Sample Outputs:
2
3
3 1 4

0
0

2
2
1 2

Solution (Python):
import os
import collections

def ints():
    bytes = bytearray()
    while True:
        buffer = os.read(0, 1024)
        if len(buffer) == 0:
            break
        bytes.extend(buffer)
    array = collections.deque(map(int, bytes.split()))
    return array

def one_pass(pass_num):
    global ans_k, ans_b
    b_time = b_count = 0
    c_time = [0] * (n + 1)
    c_count = [0] * (n + 1)
    k = 1
    for i in a:
        if b_count < k:
            if value(i) >= k:
                b_count += 1
                b_time += time(i)
                c_count[value(i)] += 1
                c_time[value(i)] += time(i)
                if pass_num == 2 and ans_k and (value(i) >= ans_k):
                    ans_b.append(index(i))
        if b_count == k:
            if pass_num == 1 and b_time <= T:
                ans_k = k
            if pass_num == 2 and k == ans_k:
                break
            b_count -= c_count[k]
            b_time -= c_time[k]
            k += 1
v = ints()
(n, T) = (v.popleft(), v.popleft())
a = []
for i in range(n):
    (k, t) = (v.popleft(), v.popleft())
    a.append((i + 1, k, t))
index = lambda x: x[0]
value = lambda x: x[1]
time = lambda x: x[2]
a.sort(key=time)
ans_k = 0
ans_b = []
one_pass(1)
one_pass(2)
print(ans_k)
print(ans_k)
print(' '.join(map(str, ans_b)))

Solution (Python):
(n, t) = map(int, input().split())
ts = []
for i in range(n):
    (a, c) = map(int, input().split())
    ts.append((a, c, i))
ts = sorted(ts, key=lambda x: x[1])
ans = 0
ptr = 0
hc = {}
hs = {}
csize = 0
ctime = 0
for i in range(n):
    n_ans = ans + 1
    if n_ans - 1 in hc:
        csize -= hc[n_ans - 1]
        ctime -= hs[n_ans - 1]
    fail = False
    while csize < n_ans:
        if ptr == n:
            fail = True
            break
        (a, c, num) = ts[ptr]
        if a >= n_ans:
            csize += 1
            ctime += c
            if a not in hc:
                hc[a] = 0
            hc[a] += 1
            if a not in hs:
                hs[a] = 0
            hs[a] += c
        ptr += 1
        if ctime > t:
            fail = True
            break
    if fail:
        break
    else:
        ans = n_ans
print(ans)
print(ans)
tks = []
for i in range(ptr):
    if len(tks) == ans:
        break
    if ts[i][0] >= ans:
        tks.append(ts[i][2])
print(' '.join([str(i + 1) for i in tks]))

Solution (Python):
import heapq
from heapq import heappush as push_
from heapq import heappop as pop_

class heapT:

    def __init__(self, T):
        self.Q = []
        self.curT = 0
        self.maxT = T
        self.his = []

    def push(self, t, index):
        push_(self.Q, (-t, index))
        self.his.append(index)
        self.curT += t
        while self.curT > self.maxT:
            self.pop()

    def pop(self):
        (t, ind) = pop_(self.Q)
        self.his.append(ind)
        self.curT -= t * -1

    def normalize(self, length):
        while len(self.Q) > length:
            self.pop()

def solve(a, n, T):
    a = sorted(a, key=lambda x: x[0], reverse=True)
    H = heapT(T)
    max_ = -1
    pos = None
    for (ak, t, ind) in a:
        H.push(t, ind)
        H.normalize(ak)
        if len(H.Q) > max_:
            max_ = len(H.Q)
            pos = len(H.his)
    d = {}
    if pos is not None:
        for x in H.his[:pos]:
            if x not in d:
                d[x] = 1
            else:
                del d[x]
    if len(d) > 0:
        print(len(d))
        print(len(d))
        print(' '.join([str(x + 1) for x in d]))
    else:
        print('0' + '\n' + '0')
(n, T) = map(int, input().split())
a = [list(map(int, input().split())) + [_] for _ in range(n)]
solve(a, n, T)