The beginning of the misunderstanding, because always think of this cow can see a few cows this point to consider, but then the basic will explode but can not think how to optimize, the key to the Internet. On the other hand, it's good to see how many cows can be seen by the cow, plus the application on the stack is OK.
-------------------------------------------------------------------------------
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque>
#include <stack>
using namespace Std;
#define LL Long Long
const int nmax=80005;
ll N,a[nmax];
stack<ll>q;
int main () {
scanf ("%lld", &n);
for (ll i=1;i<=n;i++)
scanf ("%lld", &a[i]);
Q.push (a[1]);
ll Ans=0,cur=1;
for (ll i=2;i<=n;i++) {
if (A[i]>=q.top ()) {
while (!q.empty () &&a[i]>=q.top ()) {
Q.pop ();
cur--;
}
Q.push (A[i]);
Ans+=cur;
cur++;
}
else{
Ans+=cur;
Q.push (A[i]);
cur++;
}
}
printf ("%lld\n", ans);
return 0;
}
-----------------------------------------------------------------------------------
bzoj1660: Hair Festival