傻叉单调栈

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
char sh[15];
void print(int x){
int cnt=0;
while(x) sh[++cnt]=x%10,x/=10;
dwn(i,cnt,1) putchar(sh[i]+48);
putchar(32);
}
const int nmax=2e5+5;
const int inf=0x7f7f7f7f;
int a[nmax],ans[nmax],l[nmax],r[nmax],q[nmax];
void maxs(int &a,int b){
if(a<b) a=b;
}
int main(){
int n=read();rep(i,1,n) a[i]=read();
l[1]=1;int cur=1;q[1]=1;
rep(i,2,n){
while(a[q[cur]]>=a[i]&&cur) --cur;
l[i]=q[cur]+1;q[++cur]=i;
}
r[n]=n;cur=1;q[1]=n;q[0]=n+1;
dwn(i,n-1,1){
while(a[q[cur]]>=a[i]&&cur) --cur;
r[i]=q[cur]-1;q[++cur]=i;
}
rep(i,1,n) maxs(ans[r[i]-l[i]+1],a[i]);
int tmp=0;
dwn(i,n,1) maxs(ans[i],tmp),maxs(tmp,ans[i]);
rep(i,1,n) print(ans[i]);printf("\n");
return 0;
}

  

题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注

有n只熊。他们站成一排队伍,从左到右依次1到n编号。第i只熊的高度是ai。

一组熊指的队伍中连续的一个子段。组的大小就是熊的数目。而组的力量就是这一组熊中最小的高度。

迈克想知道对于所有的组大小为x(1 ≤ x ≤ n)的,最大力量是多少。

Input
单组测试数据。
第一行有一个整数n (1 ≤ n ≤ 2×10^5),表示熊的数目。
第二行包含n个整数以空格分开,a1, a2, ..., an (1 ≤ ai ≤ 10^9),表示熊的高度。
Output
在一行中输出n个整数,对于x从1到n,输出组大小为x的最大力量。
Input示例
10
1 2 3 4 5 4 3 2 1 6
Output示例
6 4 4 3 3 2 2 1 1 1

51nod1437 迈克步的更多相关文章

  1. 51nod1437 迈克步 单调栈

    考虑一个点作为最小值的区间$[L[i], R[i]]$ 那么这个区间的所有含$i$的子区间最小值都是$v[i]$ 因此,用单调栈求出$L[i], R[i]$后,对$R[i] - L[i] + 1$这个 ...

  2. 51nod 1437:迈克步 单调栈基础题

    1437 迈克步 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  取消关注 有n只熊.他们站成一排队伍,从左到右依次1到 ...

  3. 51nod 1437 迈克步 单调栈

    利用单调栈高效的求出,一个数a[i]在哪个区间内可作为最小值存在. 正向扫描,求出a[i]可做为最小值的区间的左边界 反向扫描,求出a[i]可作为最小值的区间的右边界 r[i] - l[i] +1 就 ...

  4. 51nod 1437 迈克步(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...

  5. 51nod 1437 迈克步

    题目链接 先利用单调栈or其他方法找到一个元素g[i]作为最小值的区间,设为[L, R]. 那么长度为R-L+1的组的最大值ans=max(ans,g[i]).但是有一个问题: 比如6这个元素是长度为 ...

  6. 51nod 1437 迈克步——单调栈

    有n只熊.他们站成一排队伍,从左到右依次1到n编号.第i只熊的高度是ai. 一组熊指的队伍中连续的一个子段.组的大小就是熊的数目.而组的力量就是这一组熊中最小的高度. 迈克想知道对于所有的组大小为x( ...

  7. 胡小兔的OI日志3 完结版

    胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...

  8. 51nod 1437

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 1437 迈克步 题目来源: CodeForces 基准时间限制: ...

随机推荐

  1. pyhton Chapter3 读文件

    使用内置函数open()打开文件,data=open("1.txt").利用data.close()关闭文件.利用data.readline()读取文件中的一行数据,然后指示读取文 ...

  2. 【五】PHP数组操作函数

    1.输出数组的结构:bool print_r(数组); $arr=array('jack','mike','tom'); print_r($arr);//Array ( [0] => jack ...

  3. iOS工程预编译文件的创建

    在搜索 添加工程名/自己的pch文件名记住加后缀  

  4. ExtJs之单选及多选框

    坚持 <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-eq ...

  5. UVA 10497 - Sweet Child Makes Trouble 高精度DP

    Children are always sweet but they can sometimes make you feel bitter. In this problem, you will see ...

  6. Windows SEH学习 x86

    windows 提供的异常处理机制实际上只是一个简单的框架.我们通常所用的异常处理(比如 C++ 的 throw.try.catch)都是编译器在系统提供的异常处理机制上进行加工了的增强版本.这里先抛 ...

  7. jmeter 302请求测试

    需求,测试url:  http://test.123.com/123.action,请求该url之后会进行302跳转,判断跳转的页面是不是http://www.haha.com. 这个其实本质还是普通 ...

  8. JQuery的第一天实战学习

    1.按照下面的工程来建: 2.新建UserVerify.html文件: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...

  9. http://blog.csdn.net/wxwzy738/article/details/16968767

    http://blog.csdn.net/wxwzy738/article/details/16968767

  10. hdu 4762 Cut the Cake (大数乘法)

    猜公式: ans=n/m^(n-1) #include<stdio.h> #include<string.h> struct BigNum { ]; int len; }; i ...