D. Fedor and coupons(贪心+优先队列)
来源:互联网 发布:mac 安装mysql tar.gz 编辑:程序博客网 时间:2024/06/16 17:50
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 ndiscount 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 kcoupons with him.
Fedor wants to choose the k coupons in such a way that the number of such products x that 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张优惠券 每张优惠券可以可以优惠 li 到 ri 区间中的商品 现在选择k张优惠
券 问能优惠k次的商品最多有多少个 怎么选择优惠券
分析:
题目的本质意思是 从n个区间中任意选取k个区间使重叠区域为k次 求出k次重叠区域的最大值 并输出所选区域的编号 分析后可发现 选取的k个区域 k次重叠区间大小 == 右端点最小值 - 左端点最大值 + 1 若所选k个区间重叠次数不为k次 得出区间大小为负数 符合题目要求 所以 按左端点将区间降序排列 用优先队列维护一个k值为区间个数 便可解决
AC代码:
- #include <stdio.h>
- #include <queue>
- #include <string.h>
- #include <algorithm>
- typedef long long ll;
- using namespace std;
- struct node{
- ll l;
- ll r;
- ll index;
- }a[300005];
- int cmp(node x,node y){
- if (x.l==y.l)
- return x.r<y.r;
- return x.l<y.l;
- }
- struct cmp2{
- bool operator()(ll x,ll y)
- {
- return x > y;
- }
- };
- int main (){
- int n,k;
- while (scanf("%d%d",&n,&k)!=EOF){
- memset(a,0,sizeof(a));
- for (int i=0;i<n;i++){
- scanf ("%lld%lld",&a[i].l,&a[i].r);
- a[i].index=i+1;
- }
- sort(a,a+n,cmp);
- priority_queue<ll,vector<ll>,cmp2> Q;
- ll left=0;
- ll right=0;
- ll len=0;
- ll ans=-1;
- for (int i=0;i<n;i++){
- Q.push(a[i].r);
- if (Q.size()>k)
- Q.pop();
- if (Q.size()==k){// 求最大重叠区域 记录左端点
- len=Q.top()-a[i].l+1;// 左端点最大值减去右端点最小值+1 == 区间长度
- //printf ("%lld####\n",len);
- }
- if(ans<len){
- ans=len;
- //记住最大左端点和最小右端点
- left=a[i].l;
- right=Q.top();
- }
- }
- printf ("%lld\n",ans);
- if (!ans){// 区间大小为0 任意选取k个区间
- printf ("1");
- for (int i=2;i<=k;i++)
- printf (" %d",i);
- continue;
- }
- for (int i=0;i<n&&k>0;i++){// 输出区间
- if (a[i].r>=right&&a[i].l<=left){
- printf ("%d ",a[i].index);
- k--;
- }
- }
- putchar('\n');
- }
- return 0;
- }
- D. Fedor and coupons (贪心+优先队列)
- D. Fedor and coupons(贪心+优先队列)
- codeforces D. Fedor and coupons 贪心+优先队列
- codeforces 754D Fedor and coupons (优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- codeforces 754D Fedor and coupons【优先队列+贪心*好题】
- codeforces 754D Fedor and coupons(优先队列)
- Codeforces 754 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) 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
- codeforces754D Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- mybatis 做 insert操作的时候返回插入的那条数据的id
- TCP、UDP、IP 协议分析
- Spring Security3实现,权限动态获取
- 二叉搜索树的第K个节点
- Spring Security教程
- D. Fedor and coupons(贪心+优先队列)
- HDU 1049 Climbing Worm
- 算法分析与设计课程14——416. Partition Equal Subset Sum
- Docker CLI操作volume
- Java IO流总结
- 【项目记录】山东大学场馆管理系统之——留言板
- spring security 3 自定义认证,授权示例
- CSS之盒子(Box)模型
- Liunx线程(一)