P2866 [USACO06NOV]Bad Hair Day S
题目:
https://www.luogu.com.cn/problem/P2866
单调栈
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=8e4+7; typedef long long ll; ll h[maxn],st[maxn],top,ans; int main() { //找到右边大于等于当前的第一个节点 int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&h[i]); for(int i=1;i<=n;i++) { while(top&&h[i]>=h[st[top]]) top--; ans+=top;//栈里面存了连续的小于当前的个数 st[++top]=i; } printf("%lld\n",ans); }
浙公网安备 33010602011771号