有趣的堆栈 51Nod - 1403

https://www.51nod.com/Challenge/Problem.html#!#problemId=1403

画个图就会发现 pop就是左括号 push就是右括号 push和pop构成一个合法括号序列

题中给的第二种序列显然是左括号的前缀和 第一种就是每个右括号的对应左括号在该右括号的左边多远

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define pb push_back
const int maxn=1e6+10;

vector <int> pre;
int ary[maxn],book[2*maxn];
int n;

template <class T>
inline void _cin(T &ret)
{
    char c;
    ret = 0;
    while((c = getchar()) < '0' || c > '9');
    while(c >= '0' && c <= '9'){
        ret = ret * 10 + (c - '0');
        c = getchar();
    }
}

int main()
{
    int i,j;
    //scanf("%d",&n);
    _cin(n);
    for(i=1;i<=n;i++){
        //scanf("%d",&ary[i]);
        _cin(ary[i]);
    }
    j=2*n;
    for(i=n;i>=1;i--){
        while(book[j]) j--;
        pre.pb(j);
        book[j-2*ary[i]-1]=1;
        j--;
    }
    for(i=1;i<=2*n;i++){
        book[i]+=book[i-1];
    }
    for(i=n-1;i>=0;i--){
        printf("%d",book[pre[i]]);
        if(i>0) printf(" ");
        else printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值