1. 程式人生 > >Codeforces Hello 2018 D. Too Easy Problems 二分+貪心

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 a

i 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 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·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

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!

相關推薦

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 out

Hello 2018 D. Too Easy Problems貪心+優先佇列)

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 p

D. Too Easy Problems二分,排序,貪心

題目大意:給出n個問題和總時間t。給出的n個問題,對於每個問題都有一個限制題數,如果答題數超過了該題的限制題數,該題不得分;以及每道題所需要花費的時間。現在要求再規定時間內儘可能的拿高分。輸出結果不唯一,即順序不唯一,以及規定時間內做的不得分的題可算可不算。思路:二分,排序,

codeforces 913D Too Easy Problems

http://www.elijahqi.win/2018/01/09/codeforces-913d-too-easy-problems/ ‎ D. Too Easy Problems time limit per test 2 seconds memo

Codeforces Hello 2019 D

題目連線:https://codeforces.com/contest/1097/problem/D 首先,對每一類質數分開討論,現在問題變成了,設\(n\)中質因子\(p\)的次數是\(i\),對於一個數\(x=p^j\),經過\(k\)輪之後它被操作出來的概率。顯然這跟\(p\)是多少無關,所以記上述情況

Codeforces Hello 2018

A. Modular Exponentiation time limit per test 1 second memory limit per test 256 m

Codeforces Hello 2018 C. Party Lemonade(思維)

Description A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lem

Codeforces Hello 2018——Party Lemonade(DP)

Note In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of

101194 D Ice Cream Tower 二分 + 貪心判斷

1.題意:給你n個冰激凌球的體積,告訴你k個球能合成一個冰激凌,合成條件是放在第一個球下面的球體積要大於等於他的兩倍。問你最多能合成多少個。 2.分析: 最少能合成0個,最多能合成n/k個。存在單調性上下界,於是我們二分合成數量。 怎麼判斷mid個能否合成呢?我們知道最

codeforces #262 DIV2 C題Present(二分+貪心

這個題是用二分列舉最小值,然後判斷能否在規定的次數內使得所有的數都達到這個值。判斷的時候要用貪心的方法判斷,從左往右遍歷,這時候需要讓每次澆花的範圍儘量向右。所以當到達一個不得不澆花的地方時,要繼續

Codeforces Avito Code Challenge 2018 D. Bookshelves

多少 -c esp greedy per str rac ont pla Codeforces Avito Code Challenge 2018 D. Bookshelves 題目連接: http://codeforces.com/contest/981/problem

2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) D. Delivery Delays 二分+最短路+DP

題目連結:https://codeforc.es/gym/101933/problem/D 題意:地圖上有 n 個位置和 m 條邊,每條邊連線 u、v 且有一個距離 w,一共有 k 個詢問,每個詢問表示 ti 時間在位置 ui 有人下單點了披薩,而披薩店在 di 時間做好披薩可以送出去,披薩店在位置 1,送

Codeforces Round #514 (Div. 2) D Nature Reserve(二分

題意:一個圓與X軸相切,問最小的半徑,使得圓包含所有給定的點。 思路:因為圓是y軸xi相切的,設半徑為R,所以圓心肯定在y=R上,以每個點為圓心作半徑為R的圓與y=R交於l和r,那麼要求的圓的圓心肯定在這個區間,那麼列舉每一個點的l和r,這樣就可以一直縮小l和r的範圍,最後如果l<r,也就

CodeForces - 732 D - Exams 二分貪心

Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.About every day we know ex

Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

任意門:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory limit per test 25

Codeforces Round #521 (Div. 3) A 模擬 B 貪心 C模擬 D 二分 E 二分+排序

A Code: #include <bits/stdc++.h> #define LL long long using namespace std; int main(){ int T ; cin >> T ; int a , b , k ;

Codeforces Round #514 (Div. 2) D Nature Reserve(二分

題意:一個圓與X軸相切,問最小的半徑,使得圓包含所有給定的點。 思路:因為圓是y軸xi相切的,設半徑為R,所以圓心肯定在y=R上,以每個點為圓心作半徑為R的圓與y=R交於l和r,那麼要求的圓的圓心肯定在這個區間,那麼列舉每一個點的l和r,這樣就可以一直縮小l和r的範圍,最後

Codeforces Round #521 (Div. 3) A 模擬 B 貪心 C模擬 D 二分 E 二分+排序

A Code: #include <bits/stdc++.h> #define LL long long using namespace std; int main(){ int T

codeforces 749 D】【set+二分查詢+思路清晰】

There are n people taking part in auction today. The rules of auction are classical. There were n bids made, though it's not guaranteed they were from d

Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好題)

Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in t