0
点赞
收藏
分享

微信扫一扫

ZCMU—1892

8052cf60ff5c 2022-09-07 阅读 192


1892: kotomi and intersection


Time Limit: 1 Sec   Memory Limit: 128 MB

[​Submit​​][​Status​​][​Web Board​​]


Description


kotomi的英语实在是烂。



一天,kotomi摆弄着尺子。kotomi有n个尺子,尺子上有刻度。



kotomi想知道k个尺子的最大相交区间是多少。



Input


输入包含两个整数n,k(1 <= k <= n <= 300000)



接下来n行,每行两个整数l_i,r_i,表示尺子的左右刻度。左右刻度可以相等。(-10^9 <= l_i <= r_i <= 10^9)



Output


输出包含一个整数


Sample Input


4 2


1 100


40 70


120 130


125 180


Sample Output


31


【分析】

经典的区间求重...本来的话我本人是比较喜欢l和r分别排序然后两个指针直接走的...但是因为这道题限制了必须k把尺子,所以数组做会比较麻烦,就用优先队列了。

对当前第i个区间,考虑,如果当前区间的右区间比当前队列中最小的右区间大的话,那就把这把尺子插入队列,之所以要用优先队列是因为k把尺子的重叠区间肯定是最小的那一对l,r,所以重载一下操作符就好了...不重载操作符的做法就是插入队列的时候*-1....讲道理stl的容器还是非常好用的...除了某些超时的时候..


【代码】


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
struct xx{
int x,y;
}a[300100];

int cmp(const xx&q,const xx&w)
{
return q.x<w.x;
}

priority_queue<int,vector<int>,greater<int> >f;

int main()
{
int n,k;
scanf("%d%d",&n,&k);
for (int i=0;i<n;i++) scanf("%d%d",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
int ans=0;
for (int i=0;i<n;i++)
{
if (f.size()<k)
{
f.push(a[i].y);
if (f.size()==k)ans=max(ans,f.top()-a[i].x+1);
}
else
{
if (a[i].y>f.top())
{
f.pop();
f.push(a[i].y);
}
ans=max(ans,f.top()-a[i].x+1);
}
}
printf("%d\n",ans);
}



举报

相关推荐

ZCMU—A

ZCMU—1374

ZCMU—1306

ZCMU—1798

ZCMU—1776

ZCMU—1676

ZCMU—1309

ZCMU—1907

0 条评论