PAT 天梯赛 L2-014. 列车调度 【队列】

题目链接

https://www.patest.cn/contests/gplt/L2-014

思路
其实 每条火车道 都可以视为一个队列 满足队列的性质
当已经存在的队列 中 的列车序号 都小于 当前需要入队的列车序号时 或者一开始 本来就没有队列的情况下 就需要新增队列
如果目前的队列中存在列车序号 大于 当前入队的列车 那么替换那个最小的 大于当前入队列车序号的 队列序号
然后 每次更新下来 队列中的序号 还是有序的 可以直接 二分 而不用排序

比如题给的例子
9
8 4 2 5 3 9 1 6 7

存在的四条队列应该是
1 2 4 8
3 5
6 9
7

然后按列车序号递减 出队 就可以

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const double PI  = 3.14159265358979323846264338327;
const double E   = exp(1);
const double eps = 1e-6;

const int INF  = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD  = 1e9 + 7;

int main()
{
    vector <int> v;
    int n, num;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &num);
        vector <int>::iterator it;
        it = upper_bound(v.begin(), v.end(), num);
        if (it != v.end())
            (*it) = num;
        else
            v.push_back(num);
    }
    cout << v.size() << endl;
}   
posted @ 2018-03-22 08:56  Dup4  阅读(197)  评论(0编辑  收藏  举报