bzoj1660 / P2866 [USACO06NOV]糟糕的一天Bad Hair Day

时间:2023-03-08 22:01:47

P2866 [USACO06NOV]糟糕的一天Bad Hair Day

奶牛题里好多单调栈.....

维护一个单调递减栈,存每只牛的高度和位置,顺便统计一下答案。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 80010
int n,tp,q[N],h[N];
long long ans;
int main(){
scanf("%d",&n);
for(int i=,v;i<=n;++i){
scanf("%d",&v);
while(tp&&h[tp]<=v) ans+=i-q[tp--]-;
q[++tp]=i; h[tp]=v;
}while(tp) ans+=n-q[tp--];//最后栈内剩下的记得算
printf("%lld",ans);
return ;
}