zoukankan      html  css  js  c++  java
  • 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;
    }   
  • 相关阅读:
    阅读笔记之FastDepth: Fast Monocular Depth Estimation on Embedded Systems
    AverageMeter一个用来记录和更新变量的工具
    Python中log的简单粗暴的设置和使用
    linux 常用命令
    flutter常用组件总结
    Docker 微服务教程
    Docker 入门教程
    Activiti工作流学习分享
    CentOS7 修改主机名
    Linux中 2>&1 的含义
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433231.html
Copyright © 2011-2022 走看看