维护一个单调递减的栈 每次家进栈首的牛必然能够被其它栈里面的牛看到
#include <cstdio> #include <cstring> using namespace std; const int maxn = 80010; typedef unsigned long long LL; int a[maxn], s[maxn]; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); int top = 0; LL ans = 0; for(int i = 1; i <= n; i++) { while(top && a[i] >= a[s[top]]) top--; ans += top; s[++top] = i; } printf("%llu ", ans); return 0; }