[POJ - 3250]Bad Hair Day

This question is the most basic monotonous stack.

Stack is the so-called monotonous, maintenance to keep it within the stack elements monotonic. With monotonous stack we can solve some specific problems in the complexity of O (n) of

This question is meant to ask you all have the right to see the cattle can see the total number of cattle.

Sample topics are explained very clearly. Directly on the code.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int top, n;
stack<int> s;
int main()
{

    long long num;
    long long ans = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%lld", &num);
        while (!s.empty() && s.top() <= num)
            s.pop();
        ans += s.size();
        s.push(num);
    }
    printf("%lld\n", ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vikyanite/p/11373714.html