http://acm.hdu.edu.cn/showproblem.php?pid=5124
题意很好理解,bc比赛的时候好逗比,想到要离散化。赛后看了别人的代码,发现根本不用的。只需要记录一下每条线段的起点和终点,起点标记-1,终点为1,sort排序后,从前往后扫一下求最大值就是答案了。这里是用到了pair,很方便的写法。把起点的另一个值标记为-1,终点为1。这是因为sort是从小到大排序的,如果有两个相同的值,可以保证先出现的就是起点。很机智的写法。
以后一定要好好学习stl,功能太强大了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include<stack>
#include <queue>
#include<map>
#define pi acos(-1.0)
#define eps 1e-6
#define inf 1<<30
#define INF 1ll<<60
#define ll long long
using namespace std;
pair<int,int> a[210010];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[i*2]=make_pair(x,-1);
a[i*2+1]=make_pair(y,1);
}
sort(a,a+2*n); //从小到大排序,first是第一key
// for(int i=0;i<n+n;i++)
// {
// cout<<a[i].first<<" "<<a[i].second<<endl;
// }
int ans=0;
int tmp=0;
for(int i=0;i<n+n;i++)
{
if(a[i].second==-1)
tmp++;
else
tmp--;
ans=max(ans,tmp);
}
printf("%d\n",ans);
// cout<<ans<<endl;
}
return 0;
}