Codeforces 913D - 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 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.

Example
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个题目,并且将n个题目的时间也给了你,问:在给定时间内,你最多得到多少分,分数的计算是你完成了m道题目,这m道题目里面由给定的a[i](1<=i<=m),如果a[i]>=m,就得到一分否则不得分

这题目很坑,从样例1就开始坑了,因为题目中说的是只要求出最多分数,然后把一定要取得取出来就看可以了,但我们看看样例1,第四个题目 2 90 对它们实际对分数有没有任何帮助?其实是没有的,因此 当a[i]<m时,直接弃掉就可以了,对总分并没有任何帮助,而且也会占用时间。

说说题解;先用贪心思想,因为要在规定时间,故我们取t小的题目,先从小到大按时间排序,然后在建立一个优先队列,优先级一定要弄清楚,优先级应该以a[i]为关键词,我就是弄错了debug半天,当a[i]==a[j]时,按时间由大到小放弃,应该放弃时间多的,往后才能做更多的题目;如果不等,按a[i]由小到大排序,小的一定时对总分没有帮助的,弃掉,当a[i]>m时,我们才能入队列,然后当a[i]<m时,直接出队列,并且将m--,当加上时间超过给定时间的时候,就可以退出循环了。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
struct node
{
    int i;
    int x;//它的值
    int t;
    friend bool operator<(node a ,node b)
    {
        //和sort函数相反
        if(a.x!=b.x) return a.x>b.x;//时间相等,排除值小的
        else return a.t<b.t;//时间由大到小剔除
    }
}a[200009];
int cmp(node a,node b)
{
    return a.t<b.t;
}
priority_queue<node> q;
int main()
{
    int i,j,n,count=0,t;
     node temp;
    scanf("%d%d",&n,&t);
    for(i=0; i<=n-1; i++)
    {
        scanf("%d%d",&a[i].x,&a[i].t);
        a[i].i=i+1;
    }
    sort(a,a+n,cmp);
    for(i=0; i<=n-1; i++)
    {
        if(t-a[i].t<0) break;
        if(a[i].x>count)
        {
            t-=a[i].t;
            q.push(a[i]);
            count++;
        }
        temp=q.top();
        //printf("%d %d\n",temp.x,count);
        while(!q.empty()&&temp.x<count)
        {
            count--;
            node temp=q.top();
            t+=temp.t;
            q.pop();
        }
    }
    printf("%d\n%d\n",count,count);
    while(!q.empty())
    {
       temp=q.top();
        q.pop();
        printf("%d ",temp.i);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值