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. ACM程序对拍

    有时候在OJ刷题目的时候,总是会遇到不知名bug,题目总不能AC,自己测试的一些数据又都能得出正确的结果,又或是直接暴力会TLE,改了算法,但是仍然WA,这时候进行程序对拍测试数据不失为一个好办法.程 ...

  2. hibernate-HQL连接查询

    和SQL查询一样,HQL也支持各种各样的连接查询, 如内连接.外连接. 实例: package Test; import static org.junit.Assert.*; import java. ...

  3. log4net配置和获取ILog实例

    名称 描述 File 文件路径,如果RollingStyle为Composite或Date,则这里设置为目录,文件名在DatePattern里设置,其他则这里要有文件名.已经扩展支持虚拟目录 Roll ...

  4. 软件产品案例分析(K米 APP)

    关于 K米 -- 的案例分析 产品 K米的APP (全国KTV点歌,手机直播,互动,交友,预订)的Android客户端 第一部分 调研,评测 评测: 上手体验 第一次用这一类的软件,之前去KTV的时候 ...

  5. Some Simple Models of Neurons

    Linear neuron: \[y=b+\sum\limits_i{x_i w_i}\] Binary threshold neuron: \[z = \sum\limits_i{x_i w_i}\ ...

  6. 常见linux命令释义(第六天)——shell环境变量

    太懒了,这几天好像得了懒癌,一点都不想写博客.后来想想,知识嘛,还是分享出来的好.第一治自己的懒癌:第二顺便巩固下自己的知识. Linux的变量分为两种,一种是系统变量,是系统一经启动,就写进内存中的 ...

  7. 关键字static(1)

    static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念.被static修 ...

  8. “我是谁?”-管理者的角色、职责与工作思路.ppt

    http://doc.mbalib.com/view/95b6a675adeaf38a2c028bd4f53f0bf6.html 管理者的三大工作重心: 任务(目标).团队.规划.

  9. angularjs中ng-selected使用方法

    ng-selected只能应用在option标签上,就像ng-submit只能应用在form标签上一样. ng-selected指令为select设置了指定的选中值,HTML规范不允许浏览器保存类似s ...

  10. SQLServer------解决IP地址登录不了数据库问题

    1.找到配置管理器,打开 2.关掉SQLExpress,打开MSSQLServer 3.配置MSSQLServer(启用Named Pipes和TCP/IP) 4.修改TCP/IP属性(端口:1433 ...