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

题意:N个区间中取K个使其交集最大。

贪心策略自己看代码吧,应该是比较清晰的。(我不会告诉你是因为我不会语言表述才没写题解的,语文太差了2333)不过贪心策略较难想到啊,想了一整天(还是太弱了!QAQ)。个人认为想到了贪心策略证明还是Too Simple的,so在此就不解释了。

本想写个随机化装个B的,没想到随机会TLE,然而只好老老实实写了,装逼不成,成傻逼了...

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
#include<ctime>
#include<cstdlib>
using namespace std; int n,k,ans,ansl,ansr;
struct sdt
{
int lef,rig,num;
}a[300005];
priority_queue<int,vector<int>,greater<int> >q; int read()
{
int x=0;char c=getchar();
while(c<48||c>57)c=getchar();
while(c>47&&c<58)x*=10,x+=c-48,c=getchar();
return x;
} bool cmp(sdt x,sdt y)
{
return x.lef<y.lef;
} double random(double start,double end)
{
return start+(end-start)*rand()/(RAND_MAX+1.0);
} int main()
{
//srand(unsigned(time(0)));
n=read();k=read();
for(int i=1;i<=n;i++)
{
cin>>a[i].lef>>a[i].rig;
a[i].num=i;
}
sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++)
{
q.push(a[i].rig);
if(i<k)continue;
else if(q.size()>k)q.pop();
int len=q.top()-a[i].lef+1;
if(len>ans)
{
ans=len;
ansl=a[i].lef;
ansr=q.top();
}
} if(ans==0)
{
printf("%d\n",0);
//bool vis[300005]={0};
while(1)
{
//int p=int(random(1,n+1));
//if(vis[p]==1)continue;
//vis[p]=1;
printf("%d ",k);
k--;
if(k==0)return 0;
}
}
else
{
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
if(a[i].lef<=ansl && a[i].rig>=ansr)
{
printf("%d ",a[i].num);
k--;
if(k==0)return 0;
}
}
}
return 23333333;
}

  

Codeforces 390Div2-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

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

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

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

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

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

  5. cf754 754D - Fedor and coupons

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

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

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

  7. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  8. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  9. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

随机推荐

  1. Mac与Linux的一个巨大不同

    就是Mac仍处在桌面市场的商业圈里,尽管它的市场很小,但是正版率却很高,而且还有专门的Mac Store提供付费下载. Linux在桌面市场几乎没有份额,也不会有人会去买它的应用软件,基本上只存在于服 ...

  2. DBNull.Value 字段的用法

    DBNull 是一个单独的类,这意味着该类只能存在此实例.它指数据库中数据为空(<NULL>)时,在.net中的值 如果数据库字段的数据缺失,则您可以使用 DBNull.Value 属性将 ...

  3. Java命令行实用工具jps和jstat

    在Linux或其他UNIX和类UNIX环境下,ps命令想必大家都不陌生,我相信也有不少同学写过 ps aux | grep java | grep -v grep | awk '{print $2}' ...

  4. nagios&pnp4nagios--yum 安装

    一. 环境: 1. centos 6.4 2. 设置hostname 并且安装好apache 3. 关闭selinux及iptables 二. 安装nagios服务器端: 1. rpm -Uvh ht ...

  5. 各开源协议BSD,GPL,LGPL,Apache 2.0,mit等简介*

    快速阅读 分类 子分类 开源约定 BSD original BSD license.FreeBSD license.Original BSD license 为所欲为 Apache Licence 2 ...

  6. 【HDOJ】1648 Keywords

    PE的注意,如果没有满足条件的不输出空格.简单模拟,暴力解. /* */ #include <iostream> #include <sstream> #include < ...

  7. Ubuntu忘记管理员密码

    Ubuntu中不小心把管理员密码忘记了,真叫人头大. 现提供一个解决方案: 1.重启 Ubuntu 系统,按 Esc 进入GRUB 菜单界面,如下图: 2.选择recovery mode. (第二个) ...

  8. Codeforces 380A - Sereja and Prefixes

    原题地址:http://codeforces.com/problemset/problem/380/A 让期末考试整的好久没有写题, 放假之后由于生病也没怎么做,新年的第一场CF也不是那么在状态,只过 ...

  9. 更改nginx默认的网页目录

    默认网站根目录为/usr/local/nginx/html,要将它改成/homw/www vi /usr/local/nginx/conf/nginx.conf 将其中的           loca ...

  10. svc 报“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。“的HTTP 错误 404.3 – Not Found

    原因:系统没有默认为IIS注册WCF服务的svc文件的MIME映射. 解决方法:管理员身份运行C:\Windows\Microsoft.NET\Framework\v3.0\Windows Commu ...