lines(最大区间和)
2015-11-07 21:48
211 查看
lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1350 Accepted Submission(s): 558
[align=left]Problem Description[/align]
John
has several lines. The lines are covered on the X axis. Let A is a
point which is covered by the most lines. John wants to know how many
lines cover A.
[align=left]Input[/align]
The first line contains a single integer T(1≤T≤100)(the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109),describing a line.
[align=left]Output[/align]
For each case, output an integer means how many lines cover A.
[align=left]Sample Input[/align]
2
5
1 2
2 2
2 4
3 4
5 1000
5
1 1
2 2
3 3
4 4
5 5
[align=left]Sample Output[/align]
3
1
官方题解:
我们可以将一条线段[xi,yi]分为两个端点xi和(yi)+1, 在xi时该点会新加入一条线段,同样的,在(yi)+1时该点会减少一条线段, 因此对于2n个端点进行排序,令xi为价值1,yi为价值-1,问题转化成了最大区间和, 因为1一定在-1之前,因此问题变成最大前缀和,我们寻找最大值就是答案,另外的, 这题可以用离散化后线段树来做。复杂度为排序的复杂度即nlgn, 另外如果用第一种做法数组应是2n,而不是n,由于各种非确定性因素我在小数据就已 经设了n=10W的点。
题解:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define mem(x,y) memset(x,y,sizeof(x)) using namespace std; typedef long long LL; const int INF=0x3f3f3f3f; const int MAXN=1e5+100; pair<int,int>pa[MAXN<<1]; int main(){ int T,N; scanf("%d",&T); while(T--){int a,b; scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%d%d",&a,&b); pa[i<<1]=make_pair(a,1); pa[i<<1|1]=make_pair(b+1,-1); } sort(pa,pa+N*2); int ans=0,cnt=0; for(int i=0;i<2*N;i++) cnt+=pa[i].second,ans=max(ans,cnt); printf("%d\n",ans); } return 0; }
相关文章推荐
- steam国服推荐组
- uva 1615——Highway
- 矩阵的压缩存储————用三元组表存储稀疏矩阵
- OpenStack CentOS7 virt-manager Windows Server 2012R2镜像
- CSS学习笔记之<!important>
- 全参考视频质量评价方法(PSNR,SSIM)以及相关数据库
- HDU1575 Tr A
- 黑马程序员-----网络编程
- git 总结
- 获取url参数
- Java 快排
- java的List接口的实现类 ArrayList,LinkedList,Vector 的区别
- 服务器固定战役非官方图
- Android 系统UI状态栏设置
- vs2013 c++智能提示不选中
- Ubuntu 14 下Spark 1.4.1 安装到eclipse 3.8
- Android button 点击变色,圆角
- hwc_procs_t,hwc_display_contents_1_t 理解
- java中的序列化与反序列化,还包括将多个对象序列化到一个文件中
- Jackson2笔记