2017 省赛选拨 火车入站 CSU 1757 模拟

时间:2022-09-10 14:51:32

1757: 火车入站

        Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 512     Solved: 135    


Description

火车站人们总是在站台等待列车进站,一个站台有火车停留的时候就不能有其他火车进入,今天有n辆火车经过,已知它们进站时间Si以及出站时间Ti,进站时间到出站时间之间火车必须有一个站台给它停靠,问让所有火车都能按时停靠,至少要安排多少个站台给这些火车

Input

第一行输入一个正整数T,表示数据组数
每组数据第一行输入一个正整数n,表示火车数量(n<=10000)
接下来n行,每行输入2个正整数Si,Ti,表示第i辆火车的进站时间和出站时间(Si<Ti<1e9)

Output

每组数据输出至少需要安排多少个站台

Sample Input

1
3
1 3
3 4
4 6

Sample Output

2

Hint

Source

3901130321
 
一道经典的模拟题
 
所用的火车进站出战的模拟方法很好
参考博客 http://blog.csdn.net/zickshen/article/details/73865385
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define maxn 10010
using namespace std;
struct node{
    int t,f;
    bool operator < (const node &tmp) const{
        if(t!=tmp.t){
            return t < tmp.t;
        }
        return f < tmp.f;
    }
};
node a[maxn*2];
int main()
{
    int T,n,tmp1,tmp2;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++){
            cin >> tmp1 >> tmp2;
            a[2*i].t = tmp1,a[2*i].f = 0;
            a[2*i+1].t = tmp2,a[2*i+1].f = 1;
        }
        sort(a,a+2*n);
        int cnt = 0,ans = 0;
        for(int i=0;i<2*n;i++){//按时间排序(开始结束都放进来)
            if(!a[i].f){
                cnt++;
            }
            else cnt--;
            if(ans < cnt){//只要曾经需要多个车站就一定要有多个车站
                ans = cnt;
            }
        }
        cout << ans << endl;
    }
    return 0;
}