CSU 1757 火车入站

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

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

#include<cstdio>
#include<vector>
#include<algorithm>
//#include<cstring>
using namespace std;

struct Node
{
    int come;
    int go;
}node;

vector<Node>v;

bool cmp(Node a, Node b)
{
    return a.go < b.go;
}

int main()
{
    int t,x,y;
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &t);
    while (t--)
    {
        v.clear();
        int n;
        scanf("%d", &n);
        for (int i = 0;i < n;i++)
        {
            scanf("%d%d", &x, &y);
            node.come = x;node.go = y;
            v.push_back(node);
        }
        sort(v.begin(), v.end(),cmp);

        int max = 0;
        for (int i = 1;i < n;i++)
        {
            if (v[i - 1].go>=v[i].come)
            {
                int now = 0;int ans = 1;
                while (i + now < n)
                {
                    if(v[i - 1].go >= v[i + now].come)ans++;now++;
                }
                if (ans > max)max = ans;
            }
        }
        printf("%d\n", max);
    }
    return 0;
}

[分析2]
看了一些网上的博客,理解了一下大神们的思路。调了一个比较好的。
其实根本不用关心一辆车进出时间,只要关心什么时间有车辆进或出。
**意思就是说,根据时间顺序来,有车进了,我们记录一下,有车出了我们记录一下,所以就是记录所有进出时间,然后排序。
注意,cmp的时候,如果时间相同,先进后出。**
自己实现一下这个思路。

[代码2]

#include<cstdio>
#include<vector>
#include<algorithm>
//#include<cstring>
using namespace std;

struct Node
{
    int time;
    int infor;
}node;

vector<Node>v;

bool cmp(Node a, Node b)
{
    if (a.time == b.time)return a.infor > b.infor;
    else return a.time < b.time;
}

int main()
{
    int t,x,y;
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &t);
    while (t--)
    {
        v.clear();
        int n;
        scanf("%d", &n);
        for (int i = 0;i < n;i++)
        {
            scanf("%d%d", &x, &y);
            node.time = x;node.infor = 1;
            v.push_back(node);
            node.time = y;node.infor = 0;
            v.push_back(node);
        }
        sort(v.begin(), v.end(),cmp);

        int ans = 0;int max = 0;
        for (int i = 0;i < 2 * n;i++)
        {
            if (v[i].infor)ans++;
            else ans--;
            if (ans > max)max = ans;
        }
        printf("%d\n", max);

    }
    return 0;
}

[后记]
不要被以前学过的思路迷惑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值