bzoj 5102: [POI2018]Prawnicy

题解：

code：

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct Node{
int l,r,num;
}a[1000010];
int n,k;
bool cmp(Node a,Node b) {return a.l<b.l;}
struct node{
int x,r;
node() {}
node(int a,int b) {x=a;r=b;}
};
bool operator < (node a,node b) {return a.r>b.r;}
priority_queue<node> q;
int main()
{
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d %d",&a[i].l,&a[i].r),a[i].num=i;
sort(a+1,a+n+1,cmp);
int ans=0,num=0,x=-1;
for(int i=1;i<=n;i++)
{
if(k==1)
{
int len=a[i].r-a[i].l;
if(ans<len) x=a[i].num,ans=len;
}
else if(num==k-1)
{
int len=min(q.top().r,a[i].r)-a[i].l;
if(ans<len) x=a[i].num,ans=len;
}
num++;q.push(node(a[i].num,a[i].r));
if(num==k) num--,q.pop();
}
printf("%d\n",ans);
while(!q.empty()) q.pop();num=0;
for(int i=1;i<=n;i++)
{
if(a[i].num==x||(x==-1&&num==k-1))
{
while(!q.empty()) printf("%d ",q.top().x),q.pop();
printf("%d",a[i].num);
return 0;
}
num++;q.push(node(a[i].num,a[i].r));
if(num==k) num--,q.pop();
}
}