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. 排查Java线上服务故障的方法和实例分析

    前言 作为在线系统负责人或者是一个技术专家,你可能刚刚接手一个项目就需要处理紧急故障,或者被要求帮忙处理一些紧急的故障,这个时候的情景是: (1)你可能对这个业务仅仅是听说过,而不怎么真正了解: (2 ...

  2. python学习笔记三--字典的使用

    一.基本使用: 1. 赋值:{key:value} 1.1 与列表相同处:会改变索引(键)相关联的值的改变 1.2 与列表不同处:不用考虑值的长度,而列表是有序的需要考虑末尾偏移量,超过末尾偏移量的会 ...

  3. 4418SPI2对应管脚位置

    MCU_SPICLK2 MCU_SPITXD2 MCU_SPIFRM2 MCU_SPIRXD2

  4. WinAPI—— CallNextHookEx调用下一个钩子

    CallNextHookEx(   hhk: HHOOK;    {当前钩子的句柄}   nCode: Integer; {钩子代码; 就是给下一个钩子要交待的}   wParam: WPARAM; ...

  5. Export BOM - BOMPXINQ.EXPLODER_USEREXIT API

    --======================================================================== -- Procedure    : explode ...

  6. case语句居然还可以这么用的

    直接上代码了 // switch case case语句测试.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<ios ...

  7. UVa 11572 (滑动窗口) Unique Snowflakes

    滑动窗口挺有意思的,如果符合条件右端点一直向前走,不符合的话,左端点向前走. #include <bits/stdc++.h> using namespace std; set<in ...

  8. POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT,基础)

    题意: 有一个环,环上n个点,现在在m个点对之间连一条线,线可以往圆外面绕,也可以往里面绕,问是否必定会相交? 思路: 根据所给的m条边可知,假设给的是a-b,那么a-b要么得绕环外,要么只能在环内, ...

  9. varchar与nvarchar的区别

    nvarchar可变长度的Unicode字符数据 varchar可变长度且非 Unicode 的字符数据 举例: varchar(1)   --可以插进入一个数字或者一个字母,如果要插入一个汉字改为v ...

  10. ejabberd的多域名(domain)设置

    在ejabberd中可以支持多个domain,我讲一下我的配置过程我的ejabberd系统是:ejabberd server+sql server+openldap+gateway.我总共使用了5台机 ...