【CodeForces
来源:互联网 发布:金山快盘网络错误 编辑:程序博客网 时间:2024/06/03 04:10
C - Fedor and coupons
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 has n discount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly k coupons with him.
Fedor wants to choose the k coupons in such a way that the number of such products xthat all coupons can be used with this product x 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 and k (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 integers li and ri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-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 integers p1, 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 are 31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
题意:给出n个区间的左右端点,求其中k个区间的最大重叠区域和选择的区间编号。
分析:先按照左端点从小到大排序,然后在优先队列中保持加入k个右端点,用优先队列维护加入的最小右端点值,q.top() - a[i].l + 1就是最大重叠区域。
代码如下:
#include <map>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>#define LL long longusing namespace std;const int MX = 3e5 + 5;const int mod = 1e9 + 7;const int INF = 2e9 + 5;struct node{ LL l, r; int id;}a[MX];bool cmp(node a, node b){ return a.l < b.l;}int main(){ int n, k; scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++){ scanf("%I64d%I64d", &a[i].l, &a[i].r); a[i].id = i; } sort(a+1, a+1+n, cmp); LL ans = 0, l; priority_queue<int , vector<int>, greater<int> > q; for(int i = 1; i <= n; i++){ q.push(a[i].r); if(q.size() > k) q.pop(); LL len = q.top() - a[i].l + 1; if(len > ans && q.size() == k){ ans = len; l = a[i].l; } } if(!ans){ printf("0\n"); for(int i = 1; i <= k; i++){ printf("%d", i); if(i < k) printf(" "); else printf("\n"); } } else{ printf("%d\n", ans); for(int i = 1; i <= n; i++){ if(a[i].l <= l && ans + l - 1 <= a[i].r){ if(k >= 1) printf("%I64d ", a[i].id); else printf("%I64d\n", a[i].id); k--; } if(!k) break; } } return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- python与pip安装及配置环境
- [Leetcode] 377. Combination Sum IV 解题报告
- sgu224Little Queens 225. Little Knights
- SpringBoot搭建,10分钟教你完成框架SpringBoot+hibernate+jpa
- Oracle VM虚拟机 error
- 【CodeForces
- NP问题
- Android shrinkResources
- springBoot单元测试之1.4版本前后的变化
- Drawlayout
- 判断网络状态
- 1051. 复数乘法 (15)
- JDK7与JDK8中HashMap的实现
- window和windowmanager