Test instructions
A row of cows stood in a row, giving the height of a cow, and each cow could only look right, with a c[i for each cow]
C[i] For I can see how many cows, short cows see high cattle, ask all c[i] and is how much.
Ideas:
We convert, in fact, to find out how many times each cow can be seen, and apparently it can be seen by cows that are monotonically increasing to the left.
Then we maintain a monotonous stack, each time will be less than equal to its stack, then the number of elements in the stack is to see the number of it
It would be nice to insert the element into the stack.
Code:
#include "cstdlib" #include "CString" #include "Cmath" #include "Cstdio" #include "queue" #include "algorithm" #include " Iostream "#include" stack "using namespace std; #define LL __int64int main () { int n; while (scanf ("%d", &n)!=-1) { stack<int>q; ll Ans=0; while (n--) { int x; scanf ("%d", &x); while (!q.empty () && q.top () <=x) Q.pop (); Ans+=q.size (); Q.push (x); } printf ("%i64d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Monotone stack] POJ 3250 bad Hair Day