题目地址;点击打开链接

D. Fedor and coupons
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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!

Input

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.

Output

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.

Examples
input
4 2
1 100
40 70
120 130
125 180

output
31
1 2

input
3 2
1 12
15 20
25 30

output
0
1 2

input
5 2
1 10
5 15
14 50
30 70
99 100

output
21
3 4

Note

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张的交集最大。

【解析】:

贪心+优先队列。

基本思想:扫描数轴,当有k个区间段覆盖在当前点上时,

取最小右端点,和最大左端点,作差。

这个差,就可能是答案(当然可能有更大的这样的差)

按区间左端点排序,然后依次扫描左端点,并同时将相应的右端点压入优先队列。

当队列大小是k的时候,取出队列中最小值(右端点),与当前左端点作差。

然后不断更新这个差,取最大的哪一个,即为答案。

【代码】:

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node{ll l,r;int id;
}a[400000];
bool cmp(node c,node d)
{if(c.l==d.l)return c.r<d.r;return c.l<d.l;
}
int main()
{int n,k;priority_queue<int,vector<int>,greater<int> > q;while(~scanf("%d%d",&n,&k)){while(!q.empty())q.pop();for(int i=1;i<=n;i++){ll s,t;scanf("%lld%lld",&s,&t);a[i].l=s;a[i].r=t;a[i].id=i;}sort(a+1,a+n+1,cmp);//时间排序ll maxtimelenth=-1;ll left=10000000000,right=-1000000000;for(int i=1;i<=n;i++){q.push(a[i].r);if(q.size()==k){if(maxtimelenth<q.top()-a[i].l){maxtimelenth=q.top()-a[i].l;left=a[i].l;right=q.top();}q.pop();}}printf("%lld\n",maxtimelenth+1);int count=0;for(int i=1;i<=n&&count<k;i++){if(a[i].l<=left&&a[i].r>=right){printf("%d ",a[i].id);count++;}}printf("\n");}return 0;
}

codeforces D. Fedor and coupons 贪心+优先队列相关推荐

  1. CodeForces 754D Fedor and coupons

    题目链接:http://codeforces.com/contest/754/problem/D 题意:给你n个优惠券,每个优惠券是一个区间,他能给这个区间的所有商品进行打折,现让你选择k个优惠券,使 ...

  2. 贪心(优先队列) - New Year Snowmen - CodeForces - 140C

    贪心(优先队列) - New Year Snowmen - CodeForces - 140C 题意: 给定一个长度为n的正整数序列a1,a2,...,an.给定一个长度为n的正整数序列a_1,a_2 ...

  3. Codeforces - Fedor and coupons

    题目链接:Codeforces - Fedor and coupons 先对左端点升序排序. 然后枚举要选的左端点最靠右的边,此时怎么选前面的点呢?肯定是选k个最大的右端点呀. 所以可以用个权值线段树 ...

  4. CodeForces 140C New Year Snowmen (贪心+优先队列)

    题意:n个数,选三个严格下降的数为一组,求最多能选多少组,并列出每组哪些数. 题解:贪心+优先队列 最多能选多少组,那么必须贪心数量多的. 例如:1 1 2 3 4 5 如果按照数的大小排序,只能贪到 ...

  5. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  6. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  7. 1163 最高的奖励(贪心+优先队列)

    有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励.在结束时间之前完成该任务,就可以获得对应的奖励.完成每一个任务所需的时间都是1个单位时间.有时候完成所有任务是不可能的,因为时间上可能会有冲突 ...

  8. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 1 /* 2 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 3 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小, ...

  9. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

最新文章

  1. 不满你说,我最近跟Java源码杠上了...
  2. Java基础之Switch语句
  3. wxWidgets:wxMediaCtrl 示例
  4. div固定大小文字溢出自动缩小_【高并发】高并发环境下如何防止Tomcat内存溢出?看完我懂了!!
  5. 神经网络相关的笔试题目集合(一)
  6. 电脑计算机内存不够怎么办,电脑内存不足怎么办 电脑内存不足怎么解决
  7. Java 并发编程之同步工具类 Exchanger
  8. 求$N^N$的首位数字
  9. mate40升级鸿蒙系统教程,mate40鸿蒙系统怎么升级 教程如下
  10. 网站页面黑白色效果实现技巧
  11. 档案系列包括图书馆管理与服务器,基于Web的图书馆档案管理系统设计与实现.pdf...
  12. 什么是光纤?光纤的原理是什么?你能想象没有光纤通讯的世界么?
  13. 动态iptables 防火墙
  14. 什么是APS?APS+MES如何解决生产难题?
  15. 【大数据面试题】(五)Spark 相关面试题总结
  16. IDEA多级包创建不分离解决方案
  17. 入职外包一个月的感受
  18. echart 柱状图 ---- 坐标轴、网格、柱体配置
  19. 基于vue-cli快速构建
  20. SD-VI01事务创建运费成本报错:消息号 VY065 没有G/L帐目可以为装船成本条目被确定

热门文章

  1. MFC读写ini文件方法
  2. Fortify漏洞修复总结
  3. 云原生|kubernetes|部署MySQL一主多从复制集群(基于Binlog+Position的复制)
  4. 使用RMAN工具-RMAN详解
  5. Linux之WakeupCallback机制
  6. echarts的使用
  7. 《大话机器学习算法》贝叶斯—用贝叶斯计算吃火锅的概率
  8. Unity 修改Prefab实例将Transform变为RectTransform
  9. 网站主机Introduction
  10. 曾仕强的《中国式团队》读后