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 xis 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个区间的并尽可能大


话说无数神犇在我比赛的时候指点了我,但是MDZZ。。。我都没听懂啊,最后yy一个丑陋复杂度的做法。


考虑对于l为第一关键字排序,然后二分答案,可知l是单调递增的,用一个堆维护第k大的r是什么,然后判断区间大小就可以了。
 然而考完之后发现了,二分个毛线啊!

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 500010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,k,mid,anss[maxn],flag; struct data
{
llg quan,wz;
bool operator < (const data &a )const
{
return a.quan<quan;
}
}; priority_queue<data>q; struct node
{
llg l,r,wz;
}a[maxn]; bool cmp(const node&a,const node&b) {return a.l<b.l;} bool check(llg x)
{
if (!flag)
{
while (!q.empty()) q.pop();
}
else
{
flag=;
llg tail=;
while (!q.empty()) {anss[++tail]=q.top().wz; q.pop();}
}
for (llg i=;i<=n;i++)
{
data w;
w.quan=a[i].r; w.wz=a[i].wz;
q.push(w);
if (q.size()>k) q.pop();
if (q.size()==k)
{
if (q.top().quan-a[i].l+>=x) return ;
}
}
return ;
} int main()
{
yyj("D");
cin>>n>>k;
for (llg i=;i<=n;i++){ scanf("%lld%lld",&a[i].l,&a[i].r); a[i].wz=i;}
sort(a+,a+n+,cmp);
llg l=,r=(llg);
llg ans=;
while (l<=r)
{
mid=(l+r)/;
if(check(mid)) {ans=mid; l=mid+; flag=;} else {r=mid-;}
}
cout<<ans<<endl;
sort(anss+,anss+k+);
if (ans!=)
{
for (llg i=;i<=k;i++) printf("%lld ",anss[i]);
}
else
{
for (llg i=;i<=k;i++) printf("%lld ",i);
}
return ;
}

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

  1. CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. CodeForces 754D Fedor and coupons (优先队列)

    题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大. 析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点, ...

  3. CodeForces 754D Fedor and coupons ——(k段线段最大交集)

    还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...

  4. Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. cf754 754D - Fedor and coupons

    2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...

  6. 【codeforces 754D】Fedor and coupons

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)

    http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找k组区间,使得它们的公共交集最大. 思路: 在k组区间中,它们的公共交集=k组区间中右端 ...

  8. Codeforces Round #390 (Div. 2) D. Fedor and coupons

    题意:题目简化了就是要给你n个区间,然后让你选出k个区间  使得这k个区间有公共交集:问这个公共交集最大能是多少,并且输出所选的k个区间.如果有多组答案,则输出任意一种.   这题是用优先队列来处理区 ...

  9. codeforces754D Fedor and coupons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. Caused by: java.sql.SQLException: ORA-00918: column ambiguously defined

    外层t.*的时候,可能发现重复字段,所以检查内层的sql是否出现了重复字段的查询.

  2. 终端检测HTTPS服务端

    nscurl --ats-diagnostics --verbose https:ssl.tianwensm.xyz

  3. C#正则表达式判断输入日期格式是否正确

      /// <summary>        /// 是否为日期型字符串        /// </summary>        /// <param name=&qu ...

  4. js ajax同步请求造成浏览器假死的问题

    一.问题的起因 今天做一个需求遇到了这么个情况,就是用户个人中心有个功能,点击按钮,可以刷新用户当前的积分,这个肯定需要使用到ajax的同步请求了,当时喀喀喀三下五除二写玩了,大概代码如下: /** ...

  5. <table>标签隐藏内边框与外边框

    属性名称                属性值                        说明 frame                    void               不显示表格的 ...

  6. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  7. 关于在left join的on子句中限制左边表的取值时出现非期望的结果

    使用的SQL大概是这样的: select * from A left join B on A.id=B.id and A.id>10; --错误的使用 我们期望的结果集应该是 A中的id> ...

  8. How to install flashplugin on ubuntu

    sudo apt-get install flashplugin-installer

  9. Android Monkey测试(转载)

    Monkey是一款通过命令行来对我们APP进行测试的工具,可以运行在模拟器里或真机上.它向系统发送伪随机的用户事件流,实现对正应用程序进行压力测试. 官方介绍 :https://developer.a ...

  10. Spring透过ApplicationListener来触发contextrefreshedevent事件

    Spring通过ApplicationListener接口来触发contextrefreshedevent事件在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据.加载 ...