HDU 5124 lines 最大区间重叠点(离散化)

来源:互联网 发布:中国文化报 知乎 编辑:程序博客网 时间:2024/05/18 00:13

lines

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 887    Accepted Submission(s): 403

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(1T100)(the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1N105),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1XiYi109),describing a line.
Output
For each case, output an integer means how many lines cover A
Sample Input
251 2 2 22 43 45 100051 12 23 34 45 5
Sample Output
31
/*HDU 5124 最大区间重叠点(离散化) 我们可以将一条线段 [xi,yi]分为两个端点xi和(yi)+1,在xi时该点会新加入一条线段,同样的,在 (yi)+1时该点会减少一条线段,因此对于2n个端点进行排序,令xi为价值1,yi为价值-1,问题转化成了最大区间和,因为1一定在-1之前,因此问题变成最大前缀和,我们寻找最大值就是答案51 2 2 22 43 41 12 12 13 -13 -13 15 -15 -1为3 */#include<iostream>#include<stdio.h>#include<algorithm>#include<vector>using namespace std;vector< pair<int,int> > v;int main(){int t,n,i,x,max,ans;scanf("%d",&t);while(t--){v.clear();scanf("%d",&n); for(i=0;i<n;i++){scanf("%d",&x);v.push_back(make_pair(x,1));scanf("%d",&x);v.push_back(make_pair(x+1,-1));}sort(v.begin(),v.end());ans=0;max=0;for(i=0;i<v.size();i++){ans+=v[i].second;if(ans>max)max=ans;}printf("%d\n",max);}return 0;} 


1 0