其他分享
首页 > 其他分享> > 列车调度

列车调度

作者:互联网

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


 

标签:列车,set,temp,int,调度,push,include
来源: https://blog.csdn.net/I_O_fly/article/details/88821362