Description
大家都熟悉堆栈操作。一个堆栈一般有两种操作,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
这两种记录方法可以互相转化,我们的问题是,给定第二种记录方法的序列,请求出第一种记录方法的序列。
Solution
我们考虑第二种序列的第i个值x代表的含义。他指的是当前第i个数push和pop之间有多少个push和pop。那么对这之间的x+1个数(包括i)的pop,当前i的push都是有影响的,所以只要在i-x处+1。最后输出一下前缀和就好了。
Code
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
int a[maxn],n,i,t,j,k,l,x,b[maxn];
int dg(){
int x=0;char ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
return x;
}
void dg1(int x){
if (x<=9){
putchar(x+'0');return;
}
dg1(x/10);putchar(x%10+'0');
}
int main(){
scanf("%d\n",&n);
for (i=1;i<=n;i++)
x=dg(),a[i-x]++;
for (i=1;i<=n;i++)
a[i]+=a[i-1],dg1(a[i]),putchar(' ');
}