51nod-1403 有趣的堆栈

原题链接

基准时间限制:1 秒 空间限制:131072 KB 分值: 80  难度:5级算法题
 收藏
 关注
大家都熟悉堆栈操作。一个堆栈一般有两种操作,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
不知道为什么用cstdio当头文件超时,要用stdio.h

#include <stdio.h>
using namespace std;
typedef long long ll;
#define maxn 1000005
int d[maxn];
int get_int(){
	int m = 0;
	char ch = getchar();
	while(ch >= '0' && ch <= '9'){
		m = m * 10 + ch - '0';
		ch = getchar();
	}
	return m;
}
int main(){
	//freopen("in.txt", "r", stdin);
	int n, a;
	scanf("%d", &n);
	getchar();
	for(int i = 1; i <= n; i++){
	 a = get_int(); 
	 d[i-a]++;
    }
	for(int i = 1; i <= n; i++){
		d[i] += d[i-1];
		printf("%d ", d[i]);
	}
	printf("\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值