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

24 篇文章 0 订阅
5 篇文章 0 订阅

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

题意:给n个区间,要从中选k个从而使得这k个区间的交集最大。

分析:最后的交集一定是连续的一段这个没毛病,然后我们考虑枚举起点,把所有l <= 当前起点的区间都加入进来,然后可以二分右端点来求出当前起点下的最大值,用树状数组维护就可以了。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define N 300005
using namespace std;
int n,k,ans,al,ar,tot,f[2*N],L[N],R[N],x[2*N],Out[2*N];
vector<int> In[2*N];
int lowbit(int x)
{
	return x & -x;
}
void Insert(int x,int val)
{
	while(x <= tot)
	{
		f[x] += val;
		x += lowbit(x);
	}
}
int sum(int x)
{
	int ans = 0;
	while(x)
	{
		ans += f[x];
		x -= lowbit(x);
	}
	return ans;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1;i <= n;i++)
	{
		scanf("%d%d",&L[i],&R[i]);
		x[++tot] = L[i],x[++tot] = R[i];
	}
	sort(x+1,x+1+tot);
	for(int i = 1;i <= n;i++)
	{
		L[i] = lower_bound(x+1,x+1+tot,L[i]) - x;
		R[i] = lower_bound(x+1,x+1+tot,R[i]) - x;
		In[L[i]].push_back(R[i]);
		Out[R[i]]++;
	}
	for(int i = 1;i <= tot;i++)
	{
		if(i != 1) Insert(i-1,-Out[i-1]);
		for(int v : In[i]) Insert(v,1);
		int all = sum(tot);
		int l = 1,r = tot;
		while(l != r)
		{
			int mid = ((l + r)>>1) + 1;
			if(all-sum(mid-1) >= k) l = mid;
			else r = mid-1;	
		}
		if(all-sum(l-1) >= k) 
		{
			if(ans < x[l] - x[i] + 1)
			{
				ans = x[l]-x[i]+1;
				al = x[i],ar = x[l];
			}
		}
	}
	cout<<ans<<endl;
	if(!ans)
	{
		for(int i = 1;i <= k;i++) cout<<i<<" ";
		return 0;
	}
	for(int i = 1,cnt = 0;i <= n && cnt != k;i++)
	{
		if(x[L[i]] <= al && x[R[i]] >= ar)
		{
			cnt++;
			cout<<i<<" ";
		}
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值