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. Servlet课程0424(二) 通过继承GenericServlet来开发Servlet

    //这是第二种开发servlet的方法(继承GernericServlet) package com.tsinghua; import javax.servlet.GenericServlet; im ...

  2. IOS开发基础

    http://blog.csdn.net/wokenshin/article/details/50292253 1.修改UI大小 2.设置颜色 3.禁止横屏 4.点击空白处隐藏键盘 5.弹出键盘时,后 ...

  3. python lambda 用法

    可以视lambda为一个简易的函数,它不需要return,形式简单 #冒号左边是变量 #冒号右边是返回值 例: >>> def f (x): return x**2 ... > ...

  4. poj 3259 Wormholes(最短路 Bellman)

    题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...

  5. 谈谈数据库中MyISAM与InnoDB区别

    MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法.与 ...

  6. UVa 129 (回溯法) Krypton Factor

    回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...

  7. hdu4177:Super Mario

    主席树+离散化.给一段区间.多次询问[l,r]中有多少个数小于k.啊主席树用指针版写出来优美多了QAQ... #include<cstdio> #include<cstring> ...

  8. poj2752 水题

    又2b了一次…… var s:ansistring; ans,pre:..] of longint; i,k,tot:longint; procedure main; begin pre[]:=;k: ...

  9. JAX-RS入门 二 :运行

    上一节,已经成功的定义了一个REST服务,并且提供了具体的实现,不过我们还需要把它运行起来. 在上一节的装备部分,列举了必须的jar(在tomcat中运行)和可选的jar(作为一个独立的应用程序运行) ...

  10. JavaScript中定时器

    JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成.它们向任务队列添加定时任务. setTimeout() ...