Codeforces B. 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 ≤ 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.
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.
思路:
刚开始:觉得应该用贪心,但具体怎么做不清楚,只是单纯的用qsort把结构体数组先按a这个属性从小到大排序,若a相同,将时间按从大到小排序
样例1排序结果如下:
2 2 3 4 4
300 90 100 150 80
然后怎么贪心呢?先是胡思乱想,想的是从后往前,只要时间够,就加到答案里,时间不够就跳过该数,可显然不对,过了十几个样例(惊了)后终于卡住。问题出在哪?发现这个算法就是只要时间够就往上加,导致与题目的要求完全不符。那题目的意思是?
是在总时间的限制条件下,从数组中找有效元素,有效元素越多越好。有效元素就是最后找到的元素个数k要小于等于该元素的a属性值。k越大,导致对有效元素的要求越高(a要越大),最后的平衡点就是最值点。
依照上面刚开始的思路,平衡点之后可以看到在增加元素的个数是没有意义的,因为a值不会增加(已排好序)
那么该怎么做?
此处用到优先队列,类似于上面排好序的结构体数组,不过更为方便,因为是基于堆的实现。再利用二维向量,在读入数据时将a值相同的元素(ind,time)存到二维向量的一个向量中。因为题目要求分数(score)最大,i从n开始枚举,一直减少到0.
对于每个i值,代表想办法把当前情况下的分数弄到i上。将vector中的ai==i的哪一列全部加入优先队列中,再判断,如果元素多了,代表不考虑总是见的情况下,把分数弄到i是可能的。就pop出用时最大的元素,直到数量上得以满足i个有效元素;如果元素不够,说明凑不出分数为i的情况。接着判断队列中元素个数是否为i且总时间满足限制。若满足,那么分数为i的情况凑出来啦,跳出循环,可以输出答案。如果不满足,i--,看下一轮凑不凑得出分数为i的情况,直到答案或0.
知识点:priority_queue
struct node
{
int x,y;
bool operator < (const node & a) const
{
return x<a.x;
}
};
priority_queue <node> q;
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
代码:
#include <iostream>
#include <vector>
#include <queue>
#define max_n 200005
using namespace std;
vector<pair<int,int> > vec[max_n];
struct node
{
int id;
int time;
friend bool operator<(node a,node b)
{
return a.time<b.time;
}
}; priority_queue<node> que;
int n;
int T;
int main()
{
cin >> n >> T;
for(int i = ;i<n;i++)
{
int num,time;
cin >> num >> time;
vec[num].push_back(pair<int,int>(time,i));
}
int sum = ;
int score = ;
for(int i = n;i>=;i--)
{
for(int j = ;j<vec[i].size();j++)
{
node p;
p.id = vec[i][j].second+;
p.time = vec[i][j].first;
que.push(p);
sum += p.time;
}
while(que.size()>i)
{
int t= que.top().time;
que.pop();
sum -= t;
}
score = i;
if(que.size()==i&&sum<=T)
{
break;
}
} cout << que.size() << endl;
cout << score << endl;
while(que.size())
{
cout << que.top().id << " ";
que.pop();
}
return ;
}
Codeforces B. Too Easy Problems的更多相关文章
- Codeforces 913D - Too Easy Problems
913D - Too Easy Problems 思路:二分check k 代码: #include<bits/stdc++.h> using namespace std; #define ...
- 【CodeForces】913 D. Too Easy Problems
[题目]D. Too Easy Problems [题意]给定n个问题和总时限T,每个问题给定时间ti和限制ai,当解决的问题数k<=ai时问题有效,求在时限T内选择一些问题解决的最大有效问题数 ...
- 解题:AT2064 Many Easy Problems&EXNR #1 T3 两开花
题面 两道题比较像,放在一起写了,后者可以看成前者的加强版 (sto ztb orz) 先看AT那道题 考虑计算每个点的贡献,用容斥计算:每个点没有贡献当且仅当选的所有点都在以他为根时的一个子节点的子 ...
- 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT
[题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...
- AtcoderGrandContest 005 F. Many Easy Problems
$ >AtcoderGrandContest \space 005 F. Many Easy Problems<$ 题目大意 : 有一棵大小为 \(n\) 的树,对于每一个 \(k \i ...
- codeforces 727F. Polycarp's problems
题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...
- 【AGC 005F】Many Easy Problems
Description One day, Takahashi was given the following problem from Aoki: You are given a tree with ...
- D. Too Easy Problems
链接 [http://codeforces.com/group/1EzrFFyOc0/contest/913/problem/D] 题意 给你n个题目,考试时间T,对于每个问题都有一个ai,以及解决所 ...
- AtCoder - 2064 Many Easy Problems
Problem Statement One day, Takahashi was given the following problem from Aoki: You are given a tree ...
随机推荐
- [转]10 Best GTK Themes for Ubuntu 18.04
原文地址:https://omgfoss.com/10-best-gtk-themes-ubuntu-18-04/
- 【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用
环境 docker-ce-19.03.1-3.el7.x86_64 CentOS 7 一.查找.拉取镜像.启动容器1.查找镜像-docker search默认查找Docker Hub上的镜像,举例:D ...
- 【miscellaneous】编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
转发:http://blog.jobbole.com/30526/ 来源:潜行者m 的博客 编码一直是让新手头疼的问题,特别是 GBK.GB2312.UTF-8 这三个比较常见的网页编码的区别,更是让 ...
- 在ensp中的acl控制
原理 实验模拟 实验拓扑 相关参数 我们在每一台路由器上设置ospf服务,使其互相能通 下面我们配置基本ACL控制访问 配置完成后,尝试在R1上建立telent连接 但是这样设置是不安全的,只要是直连 ...
- HTML系列:js和css多种方式实现隔行变色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C++ 二叉搜索树原理及其实现
首先是概念:二叉搜索树又称二叉排序树,它具有以下的性质: 若是左子树不为空,则左子树上所有节点的值小于根节点的值 若是右子树不为空,则右子树上所有结点的值大于根节点的值 二叉搜索树的左右子树也是二叉搜 ...
- setdefault函数的用法及理解
setdefault函数的用法及理解 dict.setdefault(key, default=None) 功能:如果键不存在于字典中,将会添加该键并将default的值设为该键的默认值,如果键存在于 ...
- 全栈项目|小书架|服务器端-NodeJS+Koa2实现首页图书列表接口
通过上篇文章 全栈项目|小书架|微信小程序-首页水平轮播实现 我们实现了前端(小程序)效果图的展示,这篇文章来介绍服务器端的实现. 首页书籍信息 先来回顾一下首页书籍都有哪些信息: 从下面的图片可以看 ...
- 通过分析 WPF 的渲染脏区优化渲染性能
原文:通过分析 WPF 的渲染脏区优化渲染性能 本文介绍通过发现渲染脏区来提高渲染性能. 本文内容 脏区 Dirty Region WPF 性能套件 脏区监视 优化脏区重绘 脏区 Dirty Regi ...
- 阿里巴巴 Java 开发手册 (九) 异常日志
(一) 异常处理 1. [强制]Java 类库中定义的一类 RuntimeException 可以通过预先检查进行规避,而不应该 通过 catch 来处理,比如:IndexOutOfBoundsExc ...