luoguP2947 [USACO09MAR] Look Up S

mcr130102發表於2024-07-24

思路

直接單調棧魔板題

code

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define frep(i,x,y) for(int i=x;i>=y;i--)
const int N = 100005;
int n;
stack<int> s;
int a[N];
int f[N];
int main() {
	scanf("%d", &n);
	rep(i, 1, n)
	scanf("%d", &a[i]);
	frep(i, n, 1) {
		while (!s.empty() && a[s.top()] <= a[i]) s.pop();
		if (!s.empty()) f[i] = s.top();
		else f[i] = 0;
		s.push(i);
	}
	rep(i, 1, n) printf("%d\n", f[i]);
	return 0;
}

相關文章