POJ3250 Bad Hair Day(单调栈)

题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数。

用单调递减栈搞搞就是了。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 #define INF (1<<30)
 5 #define MAXN 88888
 6 int a[MAXN],r[MAXN],stack[MAXN],top;
 7 int main(){
 8     int n;
 9     scanf("%d",&n);
10     for(int i=1; i<=n; ++i) scanf("%d",a+i);
11     a[++n]=INF;
12     for(int i=1; i<=n; ++i){
13         while(top && a[stack[top]]<=a[i]){
14             r[stack[top]]=i-1;
15             --top;
16         }
17         stack[++top]=i;
18     }
19     long long res=0;
20     for(int i=1; i<n; ++i) res+=r[i]-i;
21     printf("%lld",res);
22     return 0;
23 } 
原文地址:https://www.cnblogs.com/WABoss/p/5232539.html