Live2D

题解 Weak in the Middle

题目传送门

Description

有一个长度为 \(n\) 的序列 \(a_{1,2,...,n}\) ,每次可以删掉 \(a_i\),当 \(\min(a_{i-1},a_{i+1})>a_i\) 时。

问每个数在多少次被删掉。\(n\le 10^5\)

Solution

不知道为什么,碰到这种类型的题总是做不出来。

可以想到的是,可以按着题意维护一个栈模拟就好了。

Code

#include <bits/stdc++.h>
using namespace std;

#define Int register int
#define MAXN 100005

template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}

int T,n,top,a[MAXN],ans[MAXN],sta[MAXN],maxn[MAXN];

void Work (){
	read (n),top = 0;
	for (Int i = 1;i <= n;++ i) read (a[i]),ans[i] = maxn[i] = 0;
	for (Int i = 1;i <= n;++ i){
		while (top > 1 && a[sta[top - 1]] > a[sta[top]] && a[sta[top]] < a[i]){
			ans[sta[top]] = max (maxn[sta[top]],maxn[sta[top - 1]]) + 1,
			chkmax (maxn[sta[top - 1]],ans[sta[top]]),-- top;
		}
		sta[++ top] = i;
	}
	for (Int i = 1;i <= n;++ i) write (ans[i]),putchar (' ');putchar ('\n');
}

signed main(){	
	read (T);
	while (T --> 0) Work ();
	return 0;
}
posted @ 2021-08-15 18:58  Dark_Romance  阅读(37)  评论(0编辑  收藏  举报