Codevs 1531 PeaksTitle Description
Description
The Rocky Mountains have n peaks, lined with words, numbered 1, 2, 3, and west from the east. The heights of each mountain are not the same. The peak height of the number i is hi.
Minor repairs go uphill from west to east. Every mountain, she looked back at her own journey through the hardships. In block I peaks, she records the number of peaks she can see on her back.
What is "can see"? If there is a j<k<i,hj
After returning home, the small repair put all si add up to get s as her trip happy value. Now that the height of the N-Mount peaks are available to you, can you calculate the happy value of minor repairs?
Enter a description
Input Description
The first line is an integer n (n<=15000).
The i+1 (1<=i<=n) line is an integer hi (hi<=109).
Output description
Output Description
Only one line: happy value.
Sample input
Sample Input
5
2
1
3
5
9
Sample output
Sample Output
5
Data range and Tips
Data Size & Hint
Description: S1=0, s2=1, s3=2, S4=1, S5=1.
Maintenance of a monotonically decreasing stack stack[].
The current element is x, the top of the stack is k, and the stack top pointer is Top,ans the total number of peaks you can see so far
If the x<k,ans+=top,x into the stack. The mountain is lower than the top of the stack, so because the stack is descending from the bottom to the top, there are several mountains in the stack, so you can see several mountains.
If the X=k,ans+=top, the mountain and the top of the mountain is the same height, set the top can see the Y mountain, in this mountain can see Y+1 Mountain (plus the top of the mountain), that is, all the mountains in the stack.
If x>k,1, ans+=top this mountain is higher than the top of the stack, because the stack from the bottom of the stack to the top of the monotonous decline, so this mountain than all the mountains in the stack, you can see all the mountains.
2, because the mountain is very high, so it will block the back of the line of sight, so to maintain the monotony of the stack, that is, from the top of the stack, if the mountain is higher than the top of the stack, stack top back stack, until the mountain is lower than the top of the hill, X and then into the stack.
It can be seen that for each location, you can see all the mountains in the current stack, so direct enumeration, Ans+=top
#include <cstdio>#include<iostream>using namespacestd;intstack[15001],top;intn,ans,x;intInit ()//read-in optimization, or you can enter directly{ intx=0;CharC=GetChar (); while(c<'0'|| C>'9') c=GetChar (); while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx;}intMain () {n=init (); for(intI=1; i<=n;i++) {x=init (); Ans+=top; if(x<stack[top-1]) stack[top++]=x;//this mountain is lower than the top of the stack. Else if(x>stack[top-1])//the mountain is taller than the top of the stack. { while(top&&x>stack[top-1]) top--;//Maintenance monotonicitystack[top++]=x; }} printf ("%d", ans); }
Codevs 1531 Peaks