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. git本地分支

    1. 新建并切换到该分支 $ git checkout -b iss53 Switched to a new branch 'iss53' 相当于: $ git branch iss53$ git c ...

  2. 导入.pch文件

    Xcode5中创建一个工程的时候,系统会自动创建一个以以工程名为名字的pch(Precompile Prefix Header)文件,开发的过程中可以将广泛使用的头文件以及宏包含在该文件下,编译器就会 ...

  3. “K米” 软件产品评测

    第一部分 调研,评测 评测: 第一次上手体验:KTV相信很多人都有去过,大部分包厢只有哦一个点歌台,相信很多人都会烦恼于一堆人挤在小小的点歌台前点歌的样子,还有些人不太好意思跑到点歌台点歌,常常是碰到 ...

  4. ubuntu 设置hostname

    永久修改hostname: # sudo vim /etc/hostname # sudo vim /etc/hosts

  5. iOS - Xcode7.3插件实效问题解决方法

    以往Xcode升级插件实效.1.关闭xcode 2.终端获取uid 3.文件中info.plist手动添加uid,现在不用了.在网上找的,特地记录一下,尤其是插件太多,不会麻烦了. 详细操作步骤: 关 ...

  6. UVALive 3989Ladies' Choice(稳定婚姻问题)

    题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...

  7. easyUI的控件

    CSS类定义: div easyui-window                               window窗口样式 属性如下: 1)       modal:是否生成模态窗口.tru ...

  8. JQuery------$.get()和$.post()传递数据的使用方法

    菜鸟教程地址: http://www.runoob.com/jquery/jquery-ref-ajax.html html(../Home/Index.cshtml) <!DOCTYPE ht ...

  9. 解决Bootstrap模态框切换时页面抖动 or页面滚动条

    Bootstrap为了让所有的页面(这里指内容溢出和不溢出)显示效果一样,采取的方法如下: 当Modal显示时,设置body -- overflow:hidden;margin-right:15px; ...

  10. js正则匹配浮点数或整数

    var pos='point(12.4 -45.423453)';var re = /([+]\d+[.]\d+|[-]\d+[.]\d+|\d+[.]\d+|[+]\d+|[-]\d+|\d+)/i ...