列车调度

L2-014 列车调度 (25 分)

火车站的列车调度铁轨的结构如下图所示。

两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入,最后从出口离开。在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入。如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度?

输入格式:

输入第一行给出一个整数N (2 ≤ N ≤10​5​​),下一行给出从1到N的整数序号的一个重排列。数字间以空格分隔。

输出格式:

在一行中输出可以将输入的列车按序号递减的顺序调离所需要的最少的铁轨条数。

输入样例:

9
8 4 2 5 3 9 1 6 7

输出样例:

4

 

这道题其实就是找有没有比我大的,有的话删除替换,没有的话新建一个。用vector超时了,改用set就过了

vector代码:

# include <iostream>
# include <cmath>
# include <cstring>
# include <string>
# include <map>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <string>
# include <queue>
# include <set>

using namespace std;
const int maxn = 1e5 + 5;
int flag[maxn];
int n;

int main(int argc, char *argv[])
{
    int n;
    scanf("%d", &n);
    vector<int> s;
    queue<int> q;
    for(int i = 0; i < n; i++)
    {
        int x;
        scanf("%d",&x);
        q.push(x);
    }
    s.push_back(q.front());
    q.pop();
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        vector<int>::iterator it = upper_bound(s.begin(), s.end(), temp);
        if(it == s.end())
        {
             s.push_back(temp);
        }
        else
        {
            s.erase(it);
            s.push_back(temp);
        }

        sort(s.begin(), s.end());
    }
    cout << s.size() << endl;

    return 0;
}


 

set代码:

# include <iostream>
# include <cmath>
# include <cstring>
# include <string>
# include <map>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <string>
# include <queue>
# include <set>

using namespace std;
const int maxn = 1e5 + 5;
int flag[maxn];
int n;

int main(int argc, char *argv[])
{
    int n;
    scanf("%d", &n);
    set<int> s;
    queue<int> q;
    for(int i = 0; i < n; i++)
    {
        int x;
        scanf("%d",&x);
        q.push(x);
    }
    s.insert(q.front());
    q.pop();
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        set<int>::iterator it = s.upper_bound(temp);
        if(it == s.end())
        {
            s.insert(temp);
        }
        else
        {
            s.erase(it);
            s.insert(temp);
        }
    }
    cout << s.size() << endl;

    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值