考点
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 8e4 + 50; int n, top, st[maxn]; ll ans;
int main() { int x; cin >> n; while (n--) { cin >> x; while (top && st[top] <= x) { ans += top - 1, --top; } st[++top] = x; } while (top) { ans += top - 1, --top; } cout << ans; return 0; }
|
思路
每头牛只能向右看,且只能看到比自己矮的,一直看到大于等于自己的停止;显然的单调栈裸题
可以发现,当单调栈收缩时,栈顶对答案的贡献就等于top - 1
;top
是当前栈内元素个数