D. Fedor and coupons
4 seconds
256 megabytes
standard input
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!
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.
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.
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 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;
}