首页 > 代码库 > hdu 5124 lines(Bestcoder Round #20)
hdu 5124 lines(Bestcoder Round #20)
lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 309 Accepted Submission(s): 145
Problem Description
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.
Input
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 integerN(1≤N≤105) ,indicating the number of lines.
Next N lines contains two integersXi and Yi(1≤Xi≤Yi≤109) ,describing a line.
Each test case begins with an integer
Next N lines contains two integers
Output
For each case, output an integer means how many lines cover A.
Sample Input
2 5 1 2 2 2 2 4 3 4 5 1000 5 1 1 2 2 3 3 4 4 5 5
Sample Output
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 <algorithm> using namespace std; const int maxn=500000+1000; struct node { int x; int cur; }p[maxn]; bool cmp(node a,node b) { return a.x<b.x; } int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); int x,y; int cou=0; for(int i=0;i<n;i++) { scanf("%d%d",&x,&y); p[cou].x=x; p[cou++].cur=1; p[cou].x=y+1; p[cou++].cur=-1; } sort(p,p+cou,cmp); int ans=0; int cnt=0; for(int i=0;i<cou;) { int j=i; for(;p[j].x==p[i].x&&j<cou;j++) { cnt=cnt+p[j].cur; } i=j; if(cnt>ans) ans=cnt; } printf("%d\n",ans); } return 0; }
hdu 5124 lines(Bestcoder Round #20)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。