数据结构 - 单调栈

数据结构 - 单调栈

题目链接:http://poj.org/problem?id=3250

代码:

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

const int N = 80010;

int n, num[N];

ll ans, high[N];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++ ) {
		scanf("%lld", &high[i]);
	}
	int l = 1, r = 1;
	high[n + 1] = 1 << 30;
	for (int i = 1; i <= n + 1; i ++ ) {
		while (l <= r && high[num[r]] <= high[i]) {
			ans += i - num[r] - 1;
			r -- ;
		}
		num[ ++ r] = i;
	}
	printf("%lld\n", ans);
	return 0;
}
posted @ 2019-11-15 07:47  筱柒_Littleseven  阅读(73)  评论(0编辑  收藏  举报