【BZOJ3401】[Usaco2009 Mar]Look Up 仰望【单调栈】

【题目链接】

省选结束后好累,随便敲个傻逼题。

从后向前维护一个单调栈,扫一遍就完了。

/* Pigonometry */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 100005, maxq = maxn;

int n, num[maxn], ans[maxn];

struct _data {
	int w, pos;
} q[maxq];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) num[i] = iread();

	int top = 0;
	for(int i = n; i >= 1; i--) {
		for(; top && q[top].w <= num[i]; top--);
		if(!top) ans[i] = 0;
		else ans[i] = q[top].pos;
		q[++top] = (_data){num[i], i};
	}

	for(int i = 1; i <= n; i++) printf("%d\n", ans[i]);
	return 0;
}


你可能感兴趣的:(【BZOJ3401】[Usaco2009 Mar]Look Up 仰望【单调栈】)