Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing
in a line and they are numbered from 1 to n from
left to right. i-th bear is exactly ai feet
high.

A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strengthof
a group is the minimum height of the bear in that group.

Mike is a curious to know for each x such that 1 ≤ x ≤ n the
maximum strength among all groups of size x.

Input

The first line of input contains integer n (1 ≤ n ≤ 2 × 105),
the number of bears.

The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109),
heights of bears.

Output

Print n integers in one line. For each x from 1 to n,
print the maximum strength among all groups of size x.

Sample test(s)
input
10
1 2 3 4 5 4 3 2 1 6
output

6 4 4 3 3 2 2 1 1 1

这题可以用单调栈做,维护一个栈,记录minmum(该区间的最小值)和count(区间的总长度)。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 200060
int ans[maxn];
struct node{
int count,minmum;
}stack[maxn];
int main()
{
int n,m,i,j,top,count,b;
while(scanf("%d",&n)!=EOF)
{
memset(ans,0,sizeof(ans));
top=0;
for(i=1;i<=n;i++){
scanf("%d",&b);
count=0;
while(top>0 && stack[top].minmum>=b){
stack[top].count+=count;
count=stack[top].count;
if(ans[count]<stack[top].minmum){
ans[count]=stack[top].minmum;
}
top--;
}
top++;
stack[top].minmum=b;
stack[top].count=count+1;
}
count=0;
while(top>0){
stack[top].count+=count;
count=stack[top].count;
if(ans[count]<stack[top].minmum){
ans[count]=stack[top].minmum;
}
top--;
}
for(i=n;i>=2;i--){
if(ans[i]>ans[i-1]){/*这里算出来的ans[i]是连续长度为i的区间的最小值,但这个最小值是所有连续长度为i的区间长度的最大值,下面如果ans[i+1]比ans[i]大,那么ans[i]可以更新为ans[i+1],因为如果i+1个连续数区间的最小值的最大值是b,那么去掉一个数,一定可以做到长度为i的连续数区间的最大值是b。*/
ans[i-1]=ans[i];
}
}
for(i=1;i<=n-1;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[i]);
}
return 0;
}


最新文章

  1. 如何写出安全的API接口(参数加密+超时处理+私钥验证+Https)- 续(附demo)
  2. Java研发岗位面试归类A(附答案)
  3. mssql 2008 游标 临时表 作业批处理失败问题
  4. &lt;转&gt;技术团队新官上任之基层篇
  5. Ajax readystate 5种状态
  6. [mysql] Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column &#39;loser.tank_admin.login_ip&#39; which is not functionally dependent on columns in GROUP BY clause; this is
  7. Win2003打不开https的问题
  8. SIP入门(二):建立SIPserver
  9. HDU-3864 D_num Miller_Rabin和Pollard_rho
  10. 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
  11. RabbitMq install on Centos6.3
  12. 依赖注入和IOC
  13. AngularJS2基本构造
  14. 【安富莱】【RL-TCPnet网络教程】第10章 RL-TCPnet网络协议栈移植(FreeRTOS)
  15. python全栈开发慕课网
  16. Efuse--芯片存储
  17. NowCoder Wannafly 27E 黄魔法师 构造
  18. mac os High Sierra 升级错误
  19. 如何为DigitalOcean上的服务器配置SSH密钥
  20. thinkphp如何省略index.php

热门文章

  1. PAT甲级 1155 Heap Paths (30分) 堆模拟
  2. 关于spring-data与elasticsearch的使用,自定义repository
  3. linux最大打开文件句柄数
  4. 入门训练 - 蓝桥杯(Python实现)
  5. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerp
  6. EL&amp;Filter&amp;Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统
  7. Mybatis解决字段与属性不匹配的问题、链表查询、嵌套查询、#{}和${}的区别
  8. 前端知识(二)01-NPM包管理器-谷粒学院
  9. 量子化学Gaussian技术实战课 2021年4月9号--12号 远程在线教学
  10. 解决安装mysql动态库libstdc++.so.6、libc.so.6版本过低问题