POJ 3250 Bad Hair Day

Source: Internet
Author: User

Bad Hair Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17727 Accepted: 5981

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 on our diagrams). Therefore, cow I can see the tops of the heads of cows in front of her (namely cows I+ 1, i+2, and 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; Um 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 so is the height of the cow I.

Output

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

Sample Input

610374122

Sample Output

5

Source

Usaco 2006 November Silverso the cows are facing to the right, ox I can see the cow J when and only when i<j,h[I] > h[k], k=i, I+1, ..., J. maintenance of a monotonic stack, from left to right scan h[i], (1) when the stack is empty, the h[i] pressed into the stack, (2) when the stack is not empty, constantly pop up the top of the stack element, know the current h[i] is less than the top element of the stack, the h[i] press into the stack. In this way, h[i] is pressed into the stack before the stack size, is able to see the number of cows I cattle, the sum can be accumulated.
1#include <iostream>2#include <algorithm>3#include <map>4#include <vector>5#include <functional>6#include <string>7#include <cstring>8#include <queue>9#include <stack>Ten#include <Set> One#include <cmath> A#include <cstdio> - using namespacestd; - #defineIOS Ios_base::sync_with_stdio (False) thetypedefLong LongLL; - Const intINF =0x3f3f3f3f; - Const DoublePi=4.0*atan (1.0); -  + Const intmaxn=80000; - LL ans; + intn,h; A intMain () at { -      while(SCANF ("%d", &n)! =EOF) { -stack<int>s; -ans=0; -          for(intI=0; i<n;i++){ -scanf"%d",&h); in              while(!s.empty () &&h>=s.top ()) S.pop (); -ans+=s.size (); to S.push (h); +         } -printf"%lld\n", ans); the     } *}

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.