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