Description
Some of Farmer John ' s N cows (1≤n≤80,000) is having 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 RI Ght in our diagrams). Therefore, cow I can see the tops of the heads of cows in front of hers (namely cows i+1, i+2, and so on), as long as T Hese cows is strictly shorter than cow I.
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!
Let CI denote the number of cows whose hairstyle are visible from cow i; 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 this is the height of the cow I.
Output
Line 1: A single integer which is the sum of C1 through CN.
Sample Input
6
10
3
7
4
12
2
Sample Output
5
Need to know that each number is extended to the right in that position
Maintain an incremental monotone stack to
/************************************************************************* > File Name:poj3250.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 May 07 Thursday 18:55 31 seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL; Stack <PLL>Stintheight[80010];intr[80010];intMain () {intN while(~scanf("%d", &n)) { for(inti =1; I <= N; ++i) {scanf("%d", &height[i]); R[i] = i; } while(!st.empty ()) {St.pop (); } for(inti =1; I <= N; ++i) {if(St.empty ()) {St.push (Make_pair (height[i], i)); }Else{ while(!st.empty ()) {PLL u = st.top ();if(U.first > Height[i]) { Break; } st.pop (); R[u.second] = i-1; } st.push (Make_pair (height[i], i)); } } while(!st.empty ()) {PLL u = st.top (); St.pop (); R[u.second] = n; }unsigned Long LongAns =0; for(inti =1; I <= N; ++i) {ans + = (r[i]-i); }printf("%llu\n", ans); }return 0;}
POJ3250---Bad Hair day (monotonic stack)