51nod1437 迈克步

题面在这里

很裸的单调栈。。

计算出每个数作为最小值的区间最大长度len,然后更新ans[len],最后倒着更新一遍ans[]就好了。

很坑地要加读优输优。


/*************************************************************
	Problem: 51nod 1437 迈克步 
	User: bestFy
	Language: C++
	Result: Accepted
	Time: 312 ms
	Memory: 5596 KB
	Submit_Time: 2017/11/15 19:42:41
*************************************************************/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
typedef long long LL;

inline LL read()
{
	char ch = getchar(); LL x = 0; int op = 1;
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') op = -1;
	for (; isdigit(ch); ch = getchar()) x = x*10+ch-'0';
	return op*x;
}

inline void writeAbs(LL a)
{
	if (a >= 10) writeAbs(a/10);
	putchar('0'+a%10);
}

inline void write(LL a)
{
	if (a < 0) putchar('-');
	writeAbs(abs(a));
}

inline void writeln(LL a)
{
	write(a); puts("");
}

const int N = 200010;
int n, top;
int a[N], st[N], ans[N], L[N], R[N];

int main()
{
	n = read();
	for (int i = 1; i <= n; i ++) a[i] = read();
	st[top = 0] = 0;
	for (int i = 1; i <= n; i ++){
		while (top && a[st[top]] > a[i]) top --;
		L[i] = st[top]; st[++ top] = i;
	}
	st[top = 0] = n+1;
	for (int i = n; i >= 1; i --){
		while (top && a[st[top]] >= a[i]) top --;
		R[i] = st[top]; st[++ top] = i;
	}
	for (int i = 1; i <= n; i ++)
		ans[R[i]-L[i]-1] = max(ans[R[i]-L[i]-1], a[i]);
	for (int i = n; i >= 1; i --){
		ans[i] = max(ans[i+1], ans[i]);
	}
	for (int i = 1; i <= n; i ++) write(ans[i]), putchar(' ');
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值