zoukankan      html  css  js  c++  java
  • 有趣的堆栈

    大家都熟悉堆栈操作。一个堆栈一般有两种操作,push和pop。假设所有操作都是合法的并且最终堆栈为空。我们可以有很多方法记录堆栈的操作,
    (1) 对每个pop操作,我们记录它之前一共有多少个push操作。
    (2) 对每个pop操作,我们记录这个被Pop的元素曾经被压上了几个。
    例如:操作push, push, pop, push, push, pop, push, pop, pop, pop
    用第一种方法 记录为 2, 4, 5, 5, 5
    用第二种方法 记录为 0, 0, 0, 2, 4
    这两种记录方法可以互相转化,我们的问题是,给定第二种记录方法的序列,请求出第一种记录方法的序列。
     
    Input
    第一行一个整数n,表示序列的长度(0 < n <=1000000)
    第二行n个整数,表示第二种方法的记录。
    Output
    一行,空格分隔的n个整数,表示第一种表示方法的序列。
    Input示例
    5
    0 0 0 2 4
    Output示例
    2 4 5 5 5
    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    #define Max 1000010
    int a[Max];
    int c[Max];
    int v[Max];
    int scan()
    {
        int res=0,flag=0;
        char ch;
        if((ch=getchar())=='-')flag=1;
        else if(ch>='0'&&ch<='9')res=ch-'0';
        while((ch=getchar())>='0'&&ch<='9')res=res*10+(ch-'0');
        return flag?-res:res;
    }
    void out(int a)
    {
        if(a<0){putchar('-');a=-a;
        }
        if(a>=10)out(a/10);
        putchar(a%10+'0');
    
    }
    int main()
    {
        int n;
        int end;
        scanf("%d",&n);
        end=n;
        for(int i=1;i<=n;i++)
        {
            a[i]=scan();
        }
        for(int i=n;i>=1;i--)
        {
            int t=end-a[i];
            c[i]=end;
            v[t]=1;
            for(;v[end]==1;)
            {
                end--;
            }
        }
        for(int i=1;i<=n;i++)
        {
           out(c[i]);putchar(' ');
        }
        return 0;
    }
  • 相关阅读:
    vb combobox 用法问题总结
    VB6.0 String 用法总结
    VB6.0 GetTcpTable 使用详解
    Python 自学笔记(二)第一个程序 Hello World
    Python 自学笔记(一)环境搭建
    VB MSFlexGrid 用法
    wifi 攻破
    python核心编程-第五章-习题
    python核心编程-第五章-个人笔记
    python核心编程-第四章-习题
  • 原文地址:https://www.cnblogs.com/duanyuanzhi/p/4943547.html
Copyright © 2011-2022 走看看