☀L2-014 Train dispatch (25 minutes)

The structure of the train dispatching rail at the railway station is shown in the figure below.

At both ends are an entrance (Entrance) track and an exit (Exit) track, there is Na parallel track between them . Each train can choose any track to enter from the entrance, and finally leave from the exit. In the picture, there are 9 trains, waiting in line at the entrance in the order of {8,4,2,5,3,9,1,6,7}. If they are required to leave the exit in descending order of serial number, at least how many parallel rails are needed for dispatching?

Input format:

The first line of input gives an integer N (2 ≤  N ≤10​5​​), and the next line gives Na rearrangement of integer numbers from 1 to . The numbers are separated by spaces.

Output format:

Output in one line can move the input trains away from the minimum number of tracks required in descending order of the serial number.

Input sample:

9
8 4 2 5 3 9 1 6 7

Sample output:

4
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
set<int>s;
int main()
{
    int N;
    cin>>N;
    int m;
    while(N--)
    {
        cin>>m;
        set<int>::iterator it=s.lower_bound(m);//返回第一个大于等于key_value的定位器
        if(it==s.end())
            s.insert(m);//每次insert之后,以前保存的iterator不会失效
        /*将key_value插入到set中 ,返回值是pair<set<int>::iterator,bool>,bool标志着插入是否成功,而iterator代表插入的位置,若key_value已经在set中,则iterator表示的key_value在set中的位置。*/
        else
        {
            s.erase(*it);//删除定位器iterator指向的值
            s.insert(m);
        }
    }
    cout<<s.size()<<endl;
    return 0;
}

Reference: https://blog.csdn.net/zp1455604302/article/details/103034520?biz_id=102&utm_term=L2-014%20 train dispatch%2025 points&utm_medium=distribute.pc_search_result.none-task-blog-2~all~ sobaiduweb~default-0-103034520&spm=1018.2118.3001.4187

 

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/108963943