Codeforces Hello 2018 D. Too Easy Problems 二分+贪心

Posted ProLightsfxjh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Hello 2018 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 imeaning 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·1051 ≤ 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.


Source

Hello 2018

My Solution

题意:有m个题目,每个题目有个需要花费的时间ti,以及ai,表示只要最终过题数不超过ai这个题才count。求最大的过题数以及过了哪些题,多种答案则输出任一答案。

二分+贪心

首先把题目按照ti的为优先级排序,时间少的在前面。

然后二分答案,mid表示最终的过题数,check的时候,维护剩余的时间tmp,用cnt表示已选的题目数量,

对于题目从左向右扫,每次如果该题的ai<= mid 则 tmp -= ti,当tmp >= 0是cnt++,然后tmp <= 0时break。

之后cnt >= mid则返回真,否则返回假。

最后得出ans后再按照check的方法再跑一边就可以得到具体选的题目。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
const int MAXN = 2e5 + 8;

struct p
    int t, a, ind;
 ta[MAXN];
inline bool cmp(const p &a, const p &b)
    return a.t < b.t;



LL n, time, tmp;
inline bool check(LL mid)
    int i, cnt = 0;
    tmp = time;
    for(i = 0; i < n; i++)
        if(ta[i].a >= mid)
            tmp -= ta[i].t;
            if(tmp >= 0) cnt++;
            else break;
        
    
    if(cnt >= mid) return true;
    else return false;



vector<int> ansset;
inline bool findans(LL mid)
    int i, cnt = 0;
    tmp = time;
    for(i = 0; i < n; i++)
        if(ta[i].a >= mid)
            tmp -= ta[i].t;
            if(tmp >= 0) cnt++, ansset.push_back(ta[i].ind);
            else break;
        
        if(cnt == mid) break;
    


int main()

    #ifdef LOCAL
    freopen("d.txt", "r", stdin);
    //freopen("d.out", "w", stdout);
    int T = 4;
    while(T--)
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    LL i, a, t, ans = 0;
    cin >> n >> time;
    for(i = 0; i < n; i++)
        cin >> ta[i].a >> ta[i].t;
        ta[i].ind = i+1;

    
    sort(ta, ta + n, cmp);
    LL l = 0, r = n + 1, mid;
    while(l + 1 < r)
        mid = (l + r) >> 1;
        if(check(mid))
            ans = mid;
            l = mid;
        
        else r = mid;
    

    cout << ans << endl;
    cout << ans << endl;
    findans(ans);
    for(i = 0; i < ans; i++)
        if(i != 0) cout << " ";
        cout << ansset[i];
    
    cout << endl;


    #ifdef LOCAL
    ansset.clear();
    cout << endl;
    
    #endif // LOCAL
    return 0;

  Thank you!

                                                                                                                                             ------from ProLights

以上是关于Codeforces Hello 2018 D. Too Easy Problems 二分+贪心的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round) D. Peculiar apple-tree

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) D. Array Restoration(示例代码

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) D. Array Restoration(示例代码

Codeforces Round #454 (Div. 2, based on Technocup 2018 Elimination Round 4) D. Seating of Students [

#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

Codeforces Hello 2018