您的位置:首页 > 大数据 > 人工智能

POJ3250 Bad Hair Day(单调栈)

2016-03-01 21:14 357 查看
题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数。

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

#include<cstdio>
#include<cstring>
using namespace std;
#define INF (1<<30)
#define MAXN 88888
int a[MAXN],r[MAXN],stack[MAXN],top;
int main(){
int n;
scanf("%d",&n);
for(int i=1; i<=n; ++i) scanf("%d",a+i);
a[++n]=INF;
for(int i=1; i<=n; ++i){
while(top && a[stack[top]]<=a[i]){
r[stack[top]]=i-1;
--top;
}
stack[++top]=i;
}
long long res=0;
for(int i=1; i<n; ++i) res+=r[i]-i;
printf("%lld",res);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: