bzoj1660[Usaco2006 Nov]Bad Hair Day 乱发节

Description

Input

* Line 1: 牛的数量 N。

 * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度。

Output

* Line 1: 一个整数表示c[1] 至 c[N]的和。

Sample Input

6
10
3
7
4
12
2


输入解释:

六头牛排成一排,高度依次是 10, 3, 7, 4, 12, 2。

Sample Output

5

3+0+1+0+1=5
用栈维护就行

#include<cstdio>
inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int n,top,a;
long long ans;
int zhan[80001];
int main()
{
n=read();
for (int i=1;i<=n;i++)
 {
  a=read();
  while (top && zhan[top]<=a) top--;
  ans+=top;
  zhan[++top]=a;
 }
printf("%lld",ans);
}


——by zhber,转载请注明来源
原文地址:https://www.cnblogs.com/zhber/p/4036100.html