首页 > 代码库 > POJ 3250 Bad Hair Day 单调栈

POJ 3250 Bad Hair Day 单调栈

维护一个单调递减的栈 每次家进栈首的牛必然能够被其它栈里面的牛看到

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 80010;
typedef unsigned long long LL;
int a[maxn], s[maxn];
int main()
{
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	int top = 0;
	LL ans = 0;
	for(int i = 1; i <= n; i++)
	{
		while(top && a[i] >= a[s[top]])
			top--;
		ans += top;
		s[++top] = i;
	}	
	printf("%llu\n", ans);
	return 0;
}


POJ 3250 Bad Hair Day 单调栈