hdu 5124 line STL pair

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章