zoukankan      html  css  js  c++  java
  • 洛谷P1750《出栈序列》

    原更新时间:2018-10-06 21:28:13

    这题和栈有多少关系

    题目描述

    给定一个由n个元素构成的序列,你需要将其中的元素按顺序压入一个大小为c的栈并弹出。元素按它们的出栈顺序进行排列,会得到一个新的序列。我们知道,这样的序列会有很多种,请输出所有新序列中第一个元素最小的序列(若第一个元素最小的序列有多个,则令第二个尽可能小;若仍有多个,则令第三个最小,以此类推)。

    Input / Output 格式 & 样例

    输入格式

    第一行,两个数n,c

    第二行n个数,为序列中n个元素的值

    输出格式

    输出n个数,为满足要求的序列。

    输入样例

    6 3
    5 2 3 8 7 4
    

    输出样例

    2 3 5 4 7 8
    

    数据范围

    对于40%的数据,n<=12

    对于100%的数据,c<=n<=10000,元素大小均在2*10^9以内。

    代码实现

    /* -- Basic Headers -- */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cctype>
    #include <algorithm>
    
    /* -- STL Iterators -- */
    #include <vector>
    #include <string>
    #include <stack>
    #include <queue>
    
    /* -- External Headers -- */
    
    /* -- Defined Functions -- */
    #define For(a,x,y) for (int a = x; a <= y; ++a)
    #define Forw(a,x,y) for (int a = x; a < y; ++a)
    #define Bak(a,y,x) for (int a = y; a >= x; --a)
    
    /* -- Defined Words -- */
    
    using namespace std;
    
    namespace FastIO {
        void DEBUG(char comment[], int x) {
            cerr << comment << x << endl;
        }
    
        inline int getint() {
            int s = 0, x = 1;
            char ch = getchar();
            while (!isdigit(ch)) {
                if (ch == '-') x = -1;
                ch = getchar();
            }
            while (isdigit(ch)) {
                s = s * 10 + ch - '0';
                ch = getchar();
            }
            return s * x;
        }
        inline void __basic_putint(int x) {
            if (x < 0) {
                x = -x;
                putchar('-');
            }
            if (x >= 10) __basic_putint(x / 10);
            putchar(x % 10 + '0');
        }
    
        inline void putint(int x, char external) {
            __basic_putint(x);
            putchar(external);
        }
    }
    
    namespace Solution {
        const int MAXN = 10000 + 10;
        
        struct Stack {
            private:
                int __builtin_sequence[MAXN];
                int tail;
            
            public:
                Stack() {
                    memset(__builtin_sequence, 0, sizeof(__builtin_sequence));
                    tail = 0;
                }
            
                void push(int x) {
                    __builtin_sequence[++tail] = x;
                }
                void pop() {
                    --tail;
                }
                int top() {
                    return __builtin_sequence[tail];
                }
                bool empty() {
                    return tail == 0;
                }
                int size() {
                    return tail;
                }
        } stk; 
        
        int n, c;
        int seq[MAXN];
    }
    
    int main(int argc, char *const argv[]) {
        #ifdef HANDWER_FILE
        freopen("testdata.in", "r", stdin);
        freopen("testdata.out", "w", stdout);
        #endif
        using namespace Solution;
        using namespace FastIO;
        n = getint();
        c = getint();
        int used = 0;
        int unusedNum = 1;
        For (i, 1, n) {
            seq[i] = getint();
        }
        while (stk.size() + used < n) {
            int inQueue = stk.size();
            int origUnusedNum = unusedNum;
            int minN = 2147482333;
            int len = c - inQueue;
            for (int i = origUnusedNum; i <= n && i < origUnusedNum + len; ++i) {
                if (seq[i] < minN) {
                    unusedNum = i;
                    minN = seq[i];
                }
            }
            if (stk.empty() || minN < stk.top()) {
                For (i, origUnusedNum, unusedNum) {
                    stk.push(seq[i]);
                }
                ++unusedNum;
            } else unusedNum = origUnusedNum;
            putint(stk.top(), ' ');
            ++used;
            stk.pop();
        }
        while (!stk.empty()) {
            putint(stk.top(), ' ');
            stk.pop();
        }
        return 0;
    }
    
  • 相关阅读:
    性能
    .Net 平台下的互联网架构新思考
    bootstrap-paginator 分页插件笔记
    HTTP 报文中的 Header 字段进行身份验证
    .NET简单企业应用
    djngo快速实现--使用Bootstrap
    Knockout应用开发指南
    Linux下OpenCV的环境搭建(转)
    初识树莓派(转)
    网络名词解释
  • 原文地址:https://www.cnblogs.com/handwer/p/13816359.html
Copyright © 2011-2022 走看看