Poj 3250 Bad Hair Day (monotonous stack)

Source: Internet
Author: User

Poj 3250 Bad Hair Day (monotonous stack)
Bad Hair Day

Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:14883   Accepted:4940

Description

Some of Farmer John'sNCows (1 ≤N≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows 'heads.

Each cowIHas a specified heightHi(1 ≤Hi≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cowICan see the tops of the heads of cows in front of her (namely cowsI+ 1,I+ 2, and so on), for as long as these cows are strictly shorter than cowI.

Consider this example:

        ==       ==   -   =         Cows facing right -->=   =   == - = = == = = = = =1 2 3 4 5 6 

Cow #1 can see the hairstyle of cows #2, 3, 4
Cow #2 can see no cow's hairstyle
Cow #3 can see the hairstyle of cow #4
Cow #4 can see no cow's hairstyle
Cow #5 can see the hairstyle of cow 6
Cow #6 can see no cows at all!

LetCiDenote the number of cows whose hairstyle is visible from cowI; Please compute the sumC1 throughCN. For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

Input

Line 1: The number of cows, N.
Lines 2. N + 1: Line I+ 1 contains a single integer that is the height of cow I.

Output

Line 1: A single integer that is the sum C1 through CN.

Sample Input

610374122

Sample Output

5

Monotonous stack entry question: monotonous stack is to maintain a stack, and the elements in the stack Are monotonically increasing or decreasing.

There are n cows standing in a row, giving the height of each cow in the team, each cow can only see its lower right than it, calculate the sum of all the cows that can be seen.

When we add a new height value, if there is an element in the stack that is smaller than the newly added height value, then the cow will definitely not see this height, so it will play this element on the stack. Each time a new element is added and the pop-up operation is executed, the number of elements in the stack can be seen in this ox ".

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Typedef long ll; using namespace std; int main () {int n; ll heigh, ans; stack
     
      
S; while (scanf ("% d", & n )! = EOF) {ans = 0; cin> heweigh; s. push (heweigh); // inbound stack for (int I = 1; I
      
        > Heweigh; while (! S. empty () & s. top () <= heigh) // compare the relationship between the top elements of the stack and the newly added elements {s. pop ();} ans + = s. size (); s. push (heigh);} cout <
       

 

 

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.