现在的位置: 首页 > 综合 > 正文

bzoj1660[Usaco2006 Nov]Bad Hair Day 乱发节

2018年01月13日 ⁄ 综合 ⁄ 共 488字 ⁄ 字号 评论关闭

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);
}

抱歉!评论已关闭.