单调栈入门题(站队问题)

在这里插入图片描述

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9;
int s[N], top;
int a[N], ans[N];
int main()
{
	int n;
	scanf_s("%d", &n);
	for (int i = 1; i <= n + 1; ++i)
		scanf_s("%d", a + i);
	a[0] = inf;
	a[n + 1] = inf;
	for (int i = 1; i <= n + 1; ++i)
		if (a[i] < a[s[top]])
			s[++top] = i;
		else
		{
			while (a[i] > a[s[top]])
			{
				ans[s[top]] = i - s[top] - 1;
				top--;
			}
			s[++top] = i;
		}
	for (int i = 1; i <= n; ++i)
		printf("%d", ans[i]);
	puts("");
}
发布了7 篇原创文章 · 获赞 3 · 访问量 91

猜你喜欢

转载自blog.csdn.net/qq_45863239/article/details/104268944