Codeforces 913DToo Easy Problems (优先队列 & 贪心)
2018-01-15 10:56
627 查看
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 ≤ 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
output
input
output
input
output
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.
题目链接:http://codeforces.com/problemset/problem/913/D
题意:有n个题目,tms的时间,最后得到的有效分数的计算方式为:当前如果解决出k道题目,则有效的分数为所有分数a[i]>=k的分数的数目。求最后能得到的最高的有效分数。
思路:通过优先队列每次把所有分数为i的元素按照时间从大到下出队列知道队列中只剩下i个元素为止,如果当前SumTime<t && pq.size()==i,则当前解为最优解,。
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.
题目链接:http://codeforces.com/problemset/problem/913/D
题意:有n个题目,tms的时间,最后得到的有效分数的计算方式为:当前如果解决出k道题目,则有效的分数为所有分数a[i]>=k的分数的数目。求最后能得到的最高的有效分数。
思路:通过优先队列每次把所有分数为i的元素按照时间从大到下出队列知道队列中只剩下i个元素为止,如果当前SumTime<t && pq.size()==i,则当前解为最优解,。
#include <bits/stdc++.h> #define LL long long using namespace std; const int N = 2e5 + 10; int n,t; struct node{ int indx,time; friend bool operator<(node A, node B){ return A.time < B.time; } }; vector<pair<int,int> > vct ; priority_queue<node> pq; int main(){ scanf("%d%d",&n,&t); int score,time; for(int i = 1; i <= n; i ++){ scanf("%d%d",&score,&time); vct[score].push_back(make_pair(time,i)); } LL sum = 0; int flag = 0; for(int i = n; i > 0; i --){ for(int j = 0; j < vct[i].size(); j ++){ sum += vct[i][j].first; node no; no.indx = vct[i][j].second; no.time = vct[i][j].first; pq.push(no); } while(pq.size() > i){ sum -= pq.top().time; pq.pop(); } if(pq.size() == i && sum <= t){ printf("%d\n",i); printf("%d\n",pq.size()); printf("%d",pq.top()); pq.pop(); while(!pq.empty()){ printf(" %d",pq.top()); pq.pop(); } printf("\n"); flag = 1; break; } } if(!flag) printf("0\n0\n\n"); return 0; }
相关文章推荐
- Codeforces 913D - Too Easy Problems 【优先队列】
- Codeforces 913D - Too Easy Problems(贪心+优先队列)
- Codeforces 913D - Too Easy Problems
- 【映射 && 优先队列】CodeForces - 140C New Year Snowmen
- CodeForces 137C【贪心+优先队列】
- Codeforces 898E - Squares and not squares 【优先队列贪心】
- codeforces 835B The number on the board (优先队列,贪心)
- Codeforces 725D Contest Balloons【贪心+优先队列】
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】
- Codeforces 767E Change-free【贪心+优先队列】
- Fedor and coupons CodeForces - 754D (优先队列,贪心)
- [贪心&&优先队列]uva10954 Add All
- 【CodeForces】913 D. Too Easy Problems
- CodeForces 767E Change-free【贪心+优先队列】
- 51Nod 1163 最高的奖励(贪心+优先队列 & 并差集)
- Hello 2018 D. Too Easy Problems(贪心+优先队列)
- Vijos-P1097-合并果子(简单贪心 && 优先队列 && c++)
- D. Too Easy Problems(贪心)
- D. Too Easy Problems(二分,排序,贪心)
- Codeforces 553D Nudist Beach 优先队列 贪心