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. vulcan测试记录

    感觉这个游戏很赞,是六个里面最喜欢的一个了 1.有时候挖坑对于位置要求比较大? 2.感觉难度比较大,尤其是玩到第三关很考验啊(不过从另一个方面来说也是优点?) 3.玩到现在对于怪物吃金子的原理没有很懂 ...

  2. String转json

    一.下载json 具体到http://www.json.org/上找Java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 转载地址:http://blog.csdn.net/tax ...

  3. sql异步执行

    /// <summary> /// 按钮事件 异步执行 /// </summary> /// <param name="sender">< ...

  4. Contributing to the C++ Core Guidelines

    Contributing to the C++ Core Guidelines "Within C++ is a smaller, simpler, safer language strug ...

  5. 初识Redis(1)

    Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store). 它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希( ...

  6. WebForm Repeater: 重复器

    Repeater控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行.             Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式. ...

  7. poj 1835 宇航员

    http://poj.org/problem?id=1835 宇航员 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4802 ...

  8. 【C#进阶】override new virtual

    class Organ { public virtual void Intro() { Console.WriteLine("I'm a organ."); } public st ...

  9. oracle实例恢复之检查点队列

    chain即链. oracle中链有很多种,LRU.LRUW.checkpoint queue等,都是干什么的呢??? LRU将可用块(干净的块)串起来.LRUW将脏块串起来,指导DBWR进程率先将冷 ...

  10. Java连接SqlServer2008数据库(转)

    Java连接SqlServer2008数据库 首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 下 ...