(monotone queue) bad Hair Day--POJ--3250

Source: Internet
Author: User

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

Bad Hair Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15956 Accepted: 5391

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:lineI+1 contains a single integer which is the height of cowI.

Output

Line 1: A single integer which is the sum ofC1 throughCN.

Sample Input

610374122

Sample Output

5

Source

Usaco 2006 November Silver

#include <stdio.h>#defineN 80010intStack[n];intMain () {inttop =0, I, N, p; __int64 sum=0; scanf ("%d", &N);  for(i=1; i<=n; i++) {scanf ("%d", &p);  while(top>0&& stack[top]<=p) Top--; Sum+=top; stack[++top] =p; } printf ("%i64d\n", sum); return 0;}

(monotone queue) bad Hair Day--POJ--3250

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.