time limit per test4 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 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 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.

【题目链接】:http://codeforces.com/contest/754/problem/D

【题解】



题意:

给你N个区间,让你从中选出K个区间,要求这K个区间的并集的大小最大;

做法:

将所有的区间的左端点升序排;

然后从左到右依次处理区间;

在处理区间的过程中,维护大小为K-1的区间的右端点的一个优先队列;(队首最小,递增)

每处理到一个区间i;

如果队列的大小为K-1则更新答案ans

ans = max(ans,min(a[i].r-a[i].l+1,que.top()-a[i].l+1));

然后把a[i].r加入队列,如果队列大小大于k-1,则pop()->去掉最小的那个元素;

因为我们每次都去掉最小的右端点;

则我们在处理第i个区间的时候,肯定是尽可能的增加这个a[i].l的“价值”;

尽量用一个更大的r和它配对;

这样贪心地想一下

每次枚举的区间当然就是最大的符合要求的区间了;

因为我们枚举了每一个区间的左端点,作为最后的答案区间的左端点;

因此算法是正确的;

又根据这个题目的对称性(那个区间肯定是被k-1个左端点,k-1个右端点包围的);

所以如果从右往左,也只能得到相同的答案,因此没必要再按右端点升序排再从

右到左处理;

最后O(N)

找一下区间范围在答案区间内的K个区间就好(任意都可以,只要包括);



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%I64d",&x)
  14. typedef pair<int,int> pii;
  15. typedef pair<LL,LL> pll;
  16. //const int MAXN = x;
  17. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  18. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  19. const double pi = acos(-1.0);
  20. int n,k;
  21. vector <pair<pii,int> > v;
  22. priority_queue <int,vector <int>,greater<int> > que;
  23. int main()
  24. {
  25. //freopen("F:\\rush.txt","r",stdin);
  26. rei(n);rei(k);
  27. v.resize(n);
  28. rep1(i,0,n-1)
  29. rei(v[i].fi.fi),rei(v[i].fi.se),v[i].se=i+1;
  30. sort(v.begin(),v.end());
  31. int ans = 0,L,R;
  32. rep1(i,0,n-1)
  33. {
  34. int len = que.size();
  35. if (len==k-1)
  36. {
  37. int lim = v[i].fi.se-v[i].fi.fi+1;
  38. if (!que.empty())
  39. lim = min(lim,que.top()-v[i].fi.fi+1);
  40. if (lim>ans)
  41. {
  42. ans = lim;
  43. L = v[i].fi.fi;
  44. }
  45. }
  46. que.push(v[i].fi.se);
  47. len = que.size();
  48. if (len>k-1)
  49. que.pop();
  50. }
  51. printf("%d\n",ans);
  52. if (ans==0)
  53. for (int i = 1;i<=k;i++)
  54. printf("%d ",i);
  55. for (int i = 0;i<=n-1 && k;i++)
  56. if (v[i].fi.fi<=L && L+ans-1 <= v[i].fi.se)
  57. {
  58. k--;
  59. printf("%d ",v[i].se);
  60. }
  61. return 0;
  62. }

【codeforces 754D】Fedor and coupons的更多相关文章

  1. 【Codeforces 467D】Fedor and Essay

    Codeforces 467 D 题意:给\(m​\)个单词,以及\(n​\)个置换关系,问将\(m​\)个单词替换多次后其中所含的最少的\(R​\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. 微信小程序简单常见首页demo

    wxml <view class='index-contier'> <view class="index-left"> <view>电池剩余&l ...

  2. CISP/CISA 每日一题 18

    CISSP 每日一题(答)What is the purpose of an access review and audit? Checkto ensure that users do not hav ...

  3. Java开源电商项目比較

    这里比較的都是国外的开源项目,备选项目有: Smilehouse Workspace.Pulse.Shopizer.ofbiz.bigfish.broadleaf 1.Smilehouse Works ...

  4. Behavioral模式之Visitor模式

    1.意图 表示一个作用于某对象结构中的各元素的操作.它使你能够在不改变各元素的类的前提下定义作用于这些元素的新操作. 2.别名 无 3.动机 考虑一个编译器.他将源程序表示为一个抽象语法树.该编译器须 ...

  5. 使用IPV6

    使用IPV6 知道IPV6已经很久了,但是一直没有使用过. 我使用的IPV4网络一般是 内网-->外网-->互联网,IPV6也不外乎这样,但是对IPV6而言,必须有它的"世界&q ...

  6. 13.constexpr

    #include <iostream> using namespace std; //声明返回值为常量表达式 constexpr int get() { ; return num; } v ...

  7. metabase实施文档

    安装提前:需要安装JDK1.8以上 软件下载地址: https://metabase.com 还需要下载 ojdbc7.jar,以支持Oracle驱动 下载地址:http://www.oracle.c ...

  8. 前端面试题(计算机网络/http/https)

    (前端面试题大全,持续更新) 输入url的一系列过程 http缓存(缓存生效的情况),拓展下 get与post的异同,POST一般可以发送什么类型的文件 jsonp有什么不好的地方 http请求头(h ...

  9. GO语言学习(十二)Go 语言函数

    Go 语言函数 函数是基本的代码块,用于执行一个任务. Go 语言最少有个 main() 函数. 你可以通过函数来划分不同功能,逻辑上每个函数执行的是指定的任务. 函数声明告诉了编译器函数的名称,返回 ...

  10. Maven学习总结(18)——深入理解Maven仓库

    一.本地仓库(Local Repository) 本地仓库就是一个本机的目录,这个目录被用来存储我们项目的所有依赖(插件的jar包还有一些其他的文件),简单的说,当你build一个Maven项目的时候 ...