现在的位置: 首页 > 综合 > 正文

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

2017年11月22日 ⁄ 综合 ⁄ 共 1251字 ⁄ 字号 评论关闭

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
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
/*
HDU 5124 最大区间重叠点(离散化) 
我们可以将一条线段 [xi,yi]分为两个端点xi和(yi)+1,在xi时该点会新加入一条线段,
同样的,在 (yi)+1时该点会减少一条线段,因此对于2n个端点进行排序,
令xi为价值1,yi为价值-1,问题转化成了最大区间和,因为1一定在-1之前,
因此问题变成最大前缀和,我们寻找最大值就是答案
5
1 2 
2 2
2 4
3 4

1 1
2 1
2 1
3 -1
3 -1
3 1
5 -1
5 -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;
} 

抱歉!评论已关闭.