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 hasn 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 exactlyk coupons with him.
Fedor wants to choose the k coupons in such a way that the number of such productsx that all coupons can be used with this productx 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!
The first line contains two integers n andk (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 integersli andri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-th coupon. The coupons can be equal.
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 integersp1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose.
If there are multiple answers, print any of them.
4 2 1 100 40 70 120 130 125 180
31 1 2
3 2 1 12 15 20 25 30
0 1 2
5 2 1 10 5 15 14 50 30 70 99 100
21 3 4
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 are31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is0. Fedor can choose any two coupons in this example.
题意:给你n个区间,让你从中挑出k个区间使其公共区间最大。输出选取的区间的下标。
思路:首先读完这道题是懵逼的,因为我只会用暴力,但是这里的k是不确定的所以无法用暴力,所以我就搜了搜相关的博客。发现做法还是挺多的二分,暴力,还有优先队列,虽然说之前学了优先队列但是还是不是很理解,然后我就尝试着理解优先队列。然后竟然看懂了,,,还自己打出来了,具体解释看注释。左端点从小到大排序,优先队列维护右端点,找出k个区间的最大公共区间。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define inf 0x3f3f3f3f
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
struct node
{
int l,r;
int i;
} a[300003];
int cmp(node x,node y)
{
if(x.l==y.l)
return x.r<y.r;
else return x.l<y.l;
}
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i=0; i<n; i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].i=i+1;
}
sort(a,a+n,cmp);
priority_queue<int,vector<int>,greater<int> >q;//越小越优先
int maxx=0,l,r;
for(int i=0; i<n; i++)
{
q.push(a[i].r);//入队
int len=q.size();
if(len>k)//如果队列里大于k就删除最小的,始终保证队列中有k个元素。
{
q.pop();
} int fw=q.top()-a[i].l+1;//公共区间
if(len>=k&&fw>maxx)//求出最大的公共区间,并把他的最左最右边表示出来到最后好找包括公共区间的k个区间的下标。
{
l=a[i].l;
r=q.top();
maxx=fw;
}
}
printf("%d\n",maxx);
if(maxx)
{
//printf("#%d %d\n",l,r);
for(int i=0; i<n&&k; i++)
{
if(a[i].l<=l&&a[i].r>=r)//如果包括公共区间就输出其下标
{
//printf("%d %d\n",a[i].l,a[i].r);
printf("%d ",a[i].i);
k--;
}
}
printf("\n");
}
else
{
for(int i=0; i<k; i++)
{
//printf("###\n");
if(i==k-1)
printf("%d\n",a[i].i);
else printf("%d ",a[i].i);
}
}
}
}