http://poj.org/problem?id=3250
思路:单调栈
栈中存可以看见当前这头牛的牛(栈中有多少头牛可以看见当前这头牛)
代码:
#include <iostream> #include <cstdio> #include <string> #include <queue> #include <stack> #include <sstream> using namespace std; const int MAXN=1e5+5; const int mod=1e9+7; typedef long long ll; int a[MAXN]; stack<int>st; int main() { #ifndef ONLINE_JUDGE freopen("1.in", "r", stdin); freopen("debug.out", "w", stdout); #endif int n; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); ll ans=0; for(int i=1;i<=n;i++) { while(!st.empty()&&a[i]>=st.top())st.pop(); ans+=(ll)st.size(); st.push(a[i]); } printf("%lld ",ans); return 0; }