[Monotone stack] POJ 3250 bad Hair Day

Source: Internet
Author: User

Test instructions

A row of cows stood in a row, giving the height of a cow, and each cow could only look right, with a c[i for each cow]

C[i] For I can see how many cows, short cows see high cattle, ask all c[i] and is how much.

Ideas:

We convert, in fact, to find out how many times each cow can be seen, and apparently it can be seen by cows that are monotonically increasing to the left.

Then we maintain a monotonous stack, each time will be less than equal to its stack, then the number of elements in the stack is to see the number of it

It would be nice to insert the element into the stack.

Code:

#include "cstdlib" #include "CString" #include "Cmath" #include "Cstdio" #include "queue" #include "algorithm" #include " Iostream "#include" stack "using namespace std; #define LL __int64int main () {    int n;    while (scanf ("%d", &n)!=-1)    {        stack<int>q;        ll Ans=0;        while (n--)        {            int x;            scanf ("%d", &x);            while (!q.empty () && q.top () <=x) Q.pop ();            Ans+=q.size ();            Q.push (x);        }        printf ("%i64d\n", ans);    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Monotone stack] POJ 3250 bad Hair Day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.