codeforces hello2018 D Too Easy Problems

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 jbetween 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 ≤ n1 ≤ 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.

题目大意:

n个题目,每个题目对应一个ai和ti,让选出一种方案,时间T内答完所选的题,你的得分是选的题中满足ai >= k的数,k是选的题目总数。

思路一:考虑刚好取k个,且这k个都能得到分,显然k越小越容易满足,满足决策单调性,故很容易想到二分选择的题目数。二分时,对于每个k,找到所有ai大于k的题目,然后按时间排序,选择时间最小的k个,看是否时间和小于T。总体时间复杂度O(n*logn*logn)。

思路二:考虑递推,在k = j + 1时选择到了ai >= j + 1的最优的m个元素,他们的时间和小于T,且不能再添加元素了。在k = j时,只需考虑在ai = k的所有题目中能否替换之前选择的题目,看能否找到j个和小于T的即可。递推时用堆维护选择和待选择的元素即可,每个元素最多插入删除共四次。时间复杂度O(nlog(n))。

思路一代码:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <stack>
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <iomanip>
16 #include <cctype>
17 #include <cassert>
18 #include <bitset>
19 #include <ctime>
20 
21 using namespace std;
22 
23 #define pau system("pause")
24 #define ll long long
25 #define pii pair<int, int>
26 #define pb push_back
27 #define mp make_pair
28 #define clr(a, x) memset(a, x, sizeof(a))
29 
30 const double pi = acos(-1.0);
31 const int INF = 0x3f3f3f3f;
32 const int MOD = 1e9 + 7;
33 const double EPS = 1e-9;
34 
35 int n, T, a[200015], t[200015];
36 vector<int> ans1, ans2;
37 struct gg {
38     int t, index;
39     gg(){}
40     gg(int t, int index) : t(t), index(index) {}
41     bool operator < (const gg &g) const {
42         return t < g.t;}
43 };
44 vector<gg> b;
45 bool check(int x) {
46     b.clear();
47     for (int i = 1; i <= n; ++i) {
48         if (a[i] >= x) {
49             b.pb(gg(t[i], i));
50         }
51     }
52     sort(b.begin(), b.end());
53     ll cnt_t = 0;
54     ans2.clear();
55     for (int i = 0; i < b.size(); ++i) {
56         if (ans2.size() == x) break;
57         cnt_t += b[i].t;
58         if (cnt_t > T) break;
59         ans2.pb(b[i].index);
60     }
61     if (ans2.size() == x) {
62         ans1 = ans2;
63         return true;
64     }
65     return false;
66 }
67 int main() {
68     scanf("%d%d", &n, &T);
69     for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &t[i]);
70     int s = 0, e = n, mi;
71     while (s <= e) {
72         mi = s + e >> 1;
73         if (check(mi)) s = mi + 1;
74         else e = mi - 1;
75     }
76     printf("%d
%d
", ans1.size(), ans1.size());
77     for (int i = 0; i < ans1.size(); ++i) printf("%d ", ans1[i]);
78     return 0;
79 }

思路二代码:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <stack>
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <iomanip>
16 #include <cctype>
17 #include <cassert>
18 #include <bitset>
19 #include <ctime>
20 
21 using namespace std;
22 
23 #define pau system("pause")
24 #define ll long long
25 #define pii pair<int, int>
26 #define pb push_back
27 #define mp make_pair
28 #define clr(a, x) memset(a, x, sizeof(a))
29 
30 const double pi = acos(-1.0);
31 const int INF = 0x3f3f3f3f;
32 const int MOD = 1e9 + 7;
33 const double EPS = 1e-9;
34 
35 int T, n, k;
36 priority_queue<pii, vector<pii>, greater<pii> > que[200015];
37 priority_queue<pii, vector<pii>, less<pii> > Q;
38 int main() {
39     scanf("%d%d", &n, &T);
40     for (int i = 1; i <= n; ++i) {
41         int t, a;
42         scanf("%d%d", &a, &t);
43         que[a].push(pii(t, i));
44     }
45     int cnt_T = 0;
46     for (k = n; k; --k) {
47         int flag_add = 1, flag_evic = 1;
48         while (flag_add | flag_evic) {
49             flag_add = flag_evic = 0;
50             while (que[k].size() && Q.size() < k) {
51                 pii px = que[k].top();
52                 int x = px.first;
53                 if (cnt_T + x <= T) {
54                     cnt_T += x;
55                     que[k].pop();
56                     Q.push(px);
57                     flag_add = 1;
58                 } else {
59                     break;
60                 }
61             }
62             if (Q.size() == k) break;
63             if (Q.empty()) continue;
64             if (que[k].size()) {
65                 pii px = que[k].top(), py = Q.top();
66                 int x = px.first, y = py.first;
67                 if (x < y) {
68                     que[k].pop();
69                     Q.pop();
70                     Q.push(px);
71                     cnt_T += x - y;
72                     flag_evic = 1;
73                 }
74             }
75         }
76         if (Q.size() == k) break;
77     }
78     printf("%d
%d
", k, k);
79     for (int i = 0; i < k; ++i) {
80         printf("%d ", Q.top().second);
81         Q.pop();
82     }
83     return 0;
84 }
原文地址:https://www.cnblogs.com/BIGTOM/p/8301953.html