codeforces 754D Fedor and coupons【优先队列+贪心*好题】
来源:互联网 发布:淘宝店铺ip地址怎么查 编辑:程序博客网 时间:2024/05/18 17:23
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.
The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor hasn discount coupons, thei-th of them can be used with products wi inclusive. Today Fedor wants to take exactlyk coupons with him.
Fedor wants to choose the k coupons in such a way that the number of such productsx that all coupons can be used with this productx is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
The first line contains two integers n andk (1 ≤ k ≤ n ≤ 3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose.
Each of the next n lines contains two integersli andri ( - 109 ≤ li ≤ ri ≤ 109) — the description of thei-th coupon. The coupons can be equal.
In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.
In the second line print k distinct integersp1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose.
If there are multiple answers, print any of them.
4 21 10040 70120 130125 180
311 2
3 21 1215 2025 30
01 2
5 21 105 1514 5030 7099 100
213 4
In the first example if we take the first two coupons then all the products with ids in range[40, 70] can be bought with both coupons. There are31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is0. Fedor can choose any two coupons in this example.
题意:每个商品对应一个编号(可以为负),有n张优惠券,每张优惠券可以可以优惠 L 到 R 区间中的商品。 现在仅选择k张优惠券,问能优惠k次的商品最多有多少个,以及输出优惠的区间编号。
思路:贪心预处理一下(把左端sort一下,遍历一遍压入队列),然后用优先队列不断维护K个区间(优先右端排序,此K个区间的值 = 队顶.R - S【i】.L + 1),求更新区间的最大值就可以了;
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <queue>using namespace std;const int MAXN = 3 * 1e5 + 5;struct node {int L;int R;bool friend operator <(node x, node y) {return x.R > y.R;} }s[MAXN], ss[MAXN];bool cmp(node x, node y) {return x.L < y.L;}int main() {int n, k;priority_queue<node> que; //priority_queue<int, vector<int>, greater<int> > que; scanf("%d %d", &n, &k);for(int i = 1; i <= n; i++) {scanf("%d %d", &s[i].L, &s[i].R);ss[i] = s[i];}sort(s + 1, s + n + 1, cmp);int cnt = 0, maxn = 0, left, right;for(int i = 1; i <= n; i++) { //遍历左端,用队列维护K个区间; que.push(s[i]);if(que.size() < k) continue;node e = que.top();cnt = e.R - s[i].L + 1;if(cnt > maxn) {maxn = cnt;left = s[i].L;right = e.R;}que.pop();}printf("%d\n", maxn);if(maxn == 0) {for(int i = 1; i < k; i++) {printf("%d ", i);}printf("%d\n", k);return 0;}int ans = 0;for(int i = 1; i <= n; i++) {if(ss[i].L <= left && ss[i].R >= right) {if(ans) printf(" ");printf("%d", i);if(++ans == k) break; //可能有多于K个区间的值,注意结束输出 }}printf("\n");return 0;}
- codeforces 754D Fedor and coupons【优先队列+贪心*好题】
- codeforces D. Fedor and coupons 贪心+优先队列
- codeforces 754D Fedor and coupons(优先队列)
- codeforces 754D Fedor and coupons (优先队列)
- Codeforces 754 D Fedor and coupons【优先队列】
- D. Fedor and coupons (贪心+优先队列)
- D. Fedor and coupons(贪心+优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- Codeforces 754 D Fedor and coupons
- 【codeforces 754D】Fedor and coupons
- codeforces 754D. Fedor and coupons
- CodeForces 754D Fedor and coupons
- Codeforces 754 D Fedor and coupons
- Codeforces Round #390(Div. 2)D. Fedor and coupons【优先队列】
- D. Fedor and coupons Codeforces Round #390 (Div. 2)(优先队列)
- Codeforces Round #390 (Div. 2) D - Fedor and coupons (贪心)
- Codeforces Round #390 (Div. 2//754D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Python语法
- java知识点
- Android studio常量表达式的错误
- Android:ScrollView起始位置不是最顶部
- CI持续集成系统 jira + jenkins + gerrit
- codeforces 754D Fedor and coupons【优先队列+贪心*好题】
- Android AOP之路四 编译时注解详细讲解
- 彻底搞懂四元数
- Leetcode
- centos上部署kubernetes集群
- 堆排序原理及算法实现(Java)
- JQuery中$.ajax()方法参数详解
- 关于this kernel requires an x86-64 cpu but only detected an i686 cpu unable to boot please use a kern
- savon调用WebService服务