単調なスタック - 牛の乗客25084バート髪のデー

牛の乗客20806バート髪のデー

タイトルの意味
牛は、右の1、2、3に見て目に見えない牛5牛5の後、牛、牛を参照してください。
各牛のシークは、牛の数を見ることができます。

1 2 3 4 5 6
のOO
OO O
OOO O
OOOOOO

溶液:右部のそれぞれのみ最大に必要とされる、単調減少スタックを維持します)。

#include <cstdio>
#include <iostream>
#include <stack>

typedef long long int ll;

const int MAXN = 8e4 + 3;

using namespace std;

ll height[MAXN];

int main(){
    int N;
    ll ans = 0;
    cin >> N;
    for(int i=0;i<N;i++) cin >> height[i];
    height[N] = 1e10;
    stack<int> s;
    for(int i=0;i<=N;i++){
        while(!s.empty()&&height[s.top()] <= height[i]){
            ans += (i-s.top()-1);
            s.pop();
        }
        s.push(i);
    }
    cout << ans << endl;
    return 0;
}

おすすめ

転載: www.cnblogs.com/--zz/p/11242869.html