【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)

http://www.lydsy.com/JudgeOnline/problem.php?id=1660

单调栈裸题。。累计比每一个点高的个数即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const bool cmp(const int &a, const int &b) { return a>=b; }
const int N=80006;
int s[N], top, h[N], n;
long long ans;
 
void update(int x) {
    while(top && h[x]>=s[top]) --top;
    ans+=top;
    s[++top]=h[x];
}
 
int main() {
    read(n);
    for1(i, 1, n) read(h[i]);
    for1(i, 1, n) update(i);
    printf("%lld", ans);
    return 0;
}

 

 


 

 

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

HINT

Source

posted @ 2014-09-04 05:02  iwtwiioi  阅读(445)  评论(0编辑  收藏  举报