hm 与 zx 的故事系列1

来源:互联网 发布:织梦cms源码 编辑:程序博客网 时间:2024/06/10 02:39

Description

想当年 hm 家族还没从大象家族分离出来的时候,zx妹妹还在蚂蚁窝。话说当年大象踩了蚂蚁窝,成千上万的蚂蚁要与这只大象决战,所谓当时的战斗可想而知多不激烈。往往世事难料,蚂蚁想出了一招:把所有蚂蚁排成一队,每个蚂蚁都有一定的功力,然后每个蚂蚁都能从后面最近的比他功力大的 蚂蚁那里得到能量这个时候他能释放的能量就是那头蚂蚁的能量。如果没有相应的蚂蚁释放的能量为0问最后每头蚂蚁释放的能量为多少?

Input

第一行包含的是 n (1<=n<=1000000) 表示蚂蚁头数后面n行每行一个数 xi(0<=xi<=10^9) 表示当前队列中第i号蚂蚁的能量。

Output

n行每行一个数表示每只蚂蚁所能释放的能量

Sample Input

512345554321

Sample Output

2345000000题解:就是要输出一组序列中比当前最先大的数
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <cmath>#include <algorithm>#include <stack>using namespace std;int a[1000010];int b[1000010];int main(){int n,i;while (~scanf("%d",&n)){for (i=0;i<n;i++){scanf("%d",&a[i]);b[i]=0;}stack <int>q;stack <int>q1;q1.push(0);q.push(a[0]);int t=0;for (i=1;i<n;i++){int k=q.top();if (a[i]>k)while (!q.empty() && q.top()<a[i]){b[q1.top()]=a[i];q1.pop();q.pop();}q1.push(i);q.push(a[i]);}for (i=0;i<n;i++)printf("%d\n",b[i]);} return 0;}


	
				
		
原创粉丝点击