【BZOJ】1660:[Usaco 2006 Nov]Bad Hair Day乱発節(単調スタック)

2715 ワード

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
【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)_第1张图片
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
説明を入力:
6頭のステーキが一列に並んでいて、高さは10、3、7、4、12、2の順です.
Sample Output
5
3+0+1+0+1=5
HINT
Source
Silver