POJ 3250 Bad Hair Day (use of stacks)

Source: Internet
Author: User

http://poj.org/problem?id=3250

Bad Hair Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15985 Accepted: 5404

Description

Some of Farmer John ' s n cows (1≤ n ≤80,000) is has a bad hair day! Since Each cow was self-conscious about she messy hairstyle, FJ wants to count the number of other cows so can see the to P of other cows ' heads.

Each cow i have a specified height hi (1≤ hi ≤1,000,000,000) and is standing in a line of cows All facing east (to the right with our diagrams). Therefore, Cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, a nd so on), for as long as these cows is strictly shorter than cow I.

Consider this example:

=
=       =
=-= Cows facing right---
=   =   =
= - = = =
= = = = = =

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!

Let ci denote the number of cows whose hairstyle are visible from cow i; Please compute the sum of C1 through CN. 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 which is the height of cow I.

Output

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

Sample Input

610374122

Sample Output

5

Main topic:

n number, row from left to right, ask each number to the right of the number of smaller than he has a few, and then sum


You can think back, see how many numbers he has on his left, and then add them up.

To simulate with a stack, first number is pressed into the stack:

If the stack head element s.top () > A[i],

The stack inside the elements are larger than a[i], then the number of larger than a[i] is the number of elements inside the stack s.size ();

Otherwise, the stack Head element s.top () <= A[i], stating that s.top () does not meet the criteria (not greater than a[i])

Then let the stack first element out of the stack, continue to compare the stack within the element if not greater than a[i], let him out of the stack

Which means that the elements in the stack are larger than the a[i].


#include <stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<stack>#include<algorithm>using namespacestd;Const intN =80010; typedef __int64 LL;intA[n];intMain () {stack<int>S; intN;  while(~SCANF ("%d", &N)) {ll sum=0;  for(inti =0; I < n; i++) scanf ("%d", &A[i]); S.push (a[0]);//The first number in a stack         for(inti =1; I < n; i++)//Traverse        {             while(! S.empty () && s.top () <=A[i]) S.pop ();//out of the stackSum + =s.size (); S.push (A[i]);//into the stack} printf ("%i64d\n", sum); }    return 0;}





POJ 3250 Bad Hair Day (use of stacks)

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.