D. Mike and Feet
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 × 10^5), the number of bears.

The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 10^9), 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 
 #include<stdio.h>
#include<string.h>
#include<algorithm>
const int M = 2e5 + ;
int l[M] , r[M] , a[M] ;
int b[M] ;
int n ;
int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &n) ;
for (int i = ; i <= n ; i ++) scanf ("%d" , &a[i]) ;
a[] = ; a[n + ] = ;
memset (b , - , sizeof(b)) ;
for (int i = ; i <= n ; i ++) {
l[i] = i ;
while (a[l[i] - ] >= a[i]) l[i] = l[l[i] - ] ;
}
for (int i = n ; i >= ; i --) {
r[i] = i ;
while (a[r[i] + ] >= a[i]) r[i] = r[r[i] + ] ;
}
for (int i = ; i <= n ; i ++) {
int id = r[i] - l[i] + ;
b[id] = std::max (b[id] , a[i]);
}
for (int i = n ; i >= ; i --) {
if (b[i - ] < b[i] ) b[i - ] = b[i] ;
}
for (int i = ; i <= n ; i ++) printf ("%d%c" , b[i] , i == n ? '\n' : ' ') ;
return ;
}
/*10
1 2 3 4 5 4 3 2 1 6
*/
  1. 最质补的想法,我们先考虑a[i]能影响的区间大小为w[i],(另b[i] 为长度为i的区间的最小的最大值)

for i = 1 to n

  for j = 1 to w[i]

    b[j] = max (b[j] , a[i]);

  end

end

这样最坏情况下O(n^n),显然tle.

通过yy可以发现,区间大的可以跟新区间小的,so:

for i = 1 to n

  b[w[i]] = max (b[w[i]] , a[i]);

end

for i = n to 2

  if (b[i - 1] < b[i] )   b[i - 1] = b[i] ;

end

这样就变为O(n)了。

  2.第二个要解决的问题时我们怎样在O(n)内求出w[i] ?

令w[i] = r[i] - l[i] ;

令r[i] , l[i]分别保存a[i]所能影响大的最右 和 最左。

这样在已知l[i]时,我们便可以在O(1)时间内求出l[i + 1]了。

CF #305(Div.2) D. Mike and Feet(数学推导)的更多相关文章

  1. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  2. Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...

  4. Codeforces Round #305 (Div. 2) D. Mike and Feet

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. Codeforces Round #305 (Div. 1) B. Mike and Feet

    Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...

  6. CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

    C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)

    题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...

  8. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  9. 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

随机推荐

  1. Node.js使用Mongoose包操作MongoDB数据库

    1. 安装Mongoose npm install mongoose 2. 使用 2.1 创建连接 var mongoose = require('mongoose'); mongoose.conne ...

  2. 帝国cms栏目自定义字段首页调用

    例如:增加栏目自定义字段:chushi_bpic 用下面的灵动标签和调用: [e:loop={"select C.classid,C.classname,C.classimg,D.chush ...

  3. C#6.0特性笔记

    Visual Studio 2015的C#6.0,今天无意中看这个视频,怕忘记其中的特性,故此进行记录. public class Point { //Getter专属自动属性 public int ...

  4. MFC 文件对话框

    文件对话框的分类 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见到这两种文件对话框.例如,很多编辑软件像记事本等都有"打开"选项,选择" ...

  5. 解决 Ubuntu 无法调节屏幕亮度的问题(转)

    转http://blog.163.com/tym190@126/blog/static/8776005920143192412477/ 终端输入代码:     在打开文件中找到 GRUB_CMDLIN ...

  6. Beta版本冲刺第七天 12.13

    一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 问题困难 心得体会 胡泽善 用户评价的查看以及审核 用户详情的加入,并且修改了一些卡 ...

  7. Linux 中 17 个 tar 命令实用示例

    Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...

  8. java编程思想-java中的并发(三)

    三.终结任务 1. 在阻塞时终结 线程状态 一个线程可以处于以下四种状态之一: 1)新建(new):当线程被创建时,他只会短暂的处于这种状态.此时,他已经分配了必须的系统资源,并执行了初始化.此刻线程 ...

  9. IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较

    //代码块与代替代理的设计方法 我就以在自定义视图中(非视图控制器,不能实现视图控制功能),通过代理和代码块两种方法分别实现视图的跳转,进行对比 首先自定义了一个视图,上面有一个已经注册了得BUtto ...

  10. ORACLE ORA-01157: 无法标识/锁定数据文件

    create undo tablespace MOZI datafile 'E:\oracle\product\10.2.0\oradata\orcl\MOZI.DBF' size 2048M ext ...