ZCMU—1892
来源:互联网 发布:js日期格式化yyyymmdd 编辑:程序博客网 时间:2024/05/16 11:29
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); }
0 0
- ZCMU—1892
- ZCMU—1678
- ZCMU—1605
- ZCMU—1067
- ZCMU—1607
- ZCMU—1069
- ZCMU—1611
- ZCMU—1606
- ZCMU—1774
- ZCMU—1766
- ZCMU—1752
- ZCMU—1724
- ZCMU—1623
- ZCMU—1689
- ZCMU—1667
- ZCMU—1639
- ZCMU—1668
- ZCMU—1540
- eclipse+maven+cxf webservice demo
- 225. Implement Stack using Queues
- Vector Math for 3D Computer Graphics
- Ubuntu 下安装Composer
- table tennis超级麻烦的大模拟题
- ZCMU—1892
- glFrustum()和gluPerspective()详解
- JSON
- js正则验证手机号
- DataBinding 学习系列(5)细节上的概念强化和扩展
- [BZOJ3697][[FJ2014集训]采药人的路径][点分治]
- css vertical-align属性
- c++访问webservice(gsoap简单使用)
- PAT 1100