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 × 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 

先用ST算法处理出一段区间内最小值。二分查询出对于第i个数,以它为最小向左或向右可达的最值,得到区间长度为r-l+1。那么,对于group长度为1,2,3,....r-l+1最小值为第i个数的值。则区间更新这些组大小,取最大值。此处可以使用线段树维护。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=200050;
const int inf=1e9+107; int feet[N];
int st[N][20];
int seg[N<<2];
int ans[N];
void pushdown(int rt){
seg[rt<<1]=max(seg[rt<<1],seg[rt]);
seg[rt<<1|1]=max(seg[rt<<1|1],seg[rt]);
seg[rt]=-1;
} void ST(int num){
for(int i=1;i<=num;i++)
st[i][0]=feet[i];
for(int j=1;j<20;j++)
for(int i=1;i<=num;i++){
if(i+(1<<j)-1 <= num){
st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
} void update(int rt,int l,int r,int L,int R,int val){
if(l<=L&&R<=r){
if(val>seg[rt]) seg[rt]=val; return;
}
int m=(L+R)>>1;
pushdown(rt);
if(r<=m){
update(rt<<1,l,r,L,m,val);
}
else if(l>=m+1){
update(rt<<1|1,l,r,m+1,R,val);
}
else{
update(rt<<1,l,r,L,m,val);
update(rt<<1|1,l,r,m+1,R,val);
}
} void dfs(int rt,int L,int R){
if(L==R){
ans[L]=seg[rt];
return ;
}
pushdown(rt);
int m=(L+R)>>1;
dfs(rt<<1,L,m);
dfs(rt<<1|1,m+1,R);
} int query(int l,int r){
int k=(int)((log(r-l+1))/log(2.0));
int maxl=min(st[l][k],st[r-(1<<k)+1][k]);
return maxl;
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=(n<<2)+10;i++){
seg[i]=-1;
}
for(int i=1;i<=n;i++){
scanf("%d",&feet[i]);
}
ST(n);
for(int i=1;i<=n;i++){
//left
int l=1,r=i;
int L=i,R=i;
while(l<=r){
int m=(l+r)>>1;
int index=query(m,i);
if(index>=feet[i]){
r=m-1;
L=m;
}
else {
l=m+1;
}
}
//right
l=i,r=n;
while(l<=r){
int m=(l+r)>>1;
int index=query(i,m);
if(index>=feet[i]){
l=m+1;
R=m;
}
else {
r=m-1;
}
}
update(1,1,R-L+1,1,n,feet[i]);
}
dfs(1,1,n);
printf("%d",ans[1]);
for(int i=2;i<=n;i++)
printf(" %d",ans[i]);
printf("\n");
}
return 0;
}

  

Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)的更多相关文章

  1. Codeforces Round #603 (Div. 2) E. Editor 线段树

    E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...

  2. Codeforces Round #765 Div.1 F. Souvenirs 线段树

    题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...

  3. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  4. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  5. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  6. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  7. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  8. Codeforces Round #305 (Div. 2) E题(数论+容斥原理)

    E. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #305 (Div. 2) C题 (数论)

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

随机推荐

  1. ubuntu16.04更改源

    最近用apt-get安装软件总是提示列表无法全部更新,导致一些软件安装不上,下面我们通过讲/etc/apt/sources.list里为阿里源,实现访问. 第一步: 备份/etc/apt/source ...

  2. PHP入门开发

    1.下载开发工具phpstorm 地址:http://www.jetbrains.com/phpstorm/download/download-thanks.html?platform=windows ...

  3. 胖ap和瘦ap的区别

    一,什么是AP,胖瘦AP如何区分?       先说说AP的概念.AP是Access Point的简称,即无线接入点,其作用是把局域网里通过双绞线传输的有线信号(即电信号)经过编译,转换成无线电信号传 ...

  4. Hadoop Hive概念学习系列之hive里的桶(十一)

    不多说,直接上干货!  Hive还可以把表或分区,组织成桶.将表或分区组织成桶有以下几个目的: 第一个目的是为看取样更高效,因为在处理大规模的数据集时,在开发.测试阶段将所有的数据全部处理一遍可能不太 ...

  5. unity多语言本地化

    简介 嗯...一般来说做游戏啥的都不会只发一个国家,但是每个国家语言不同,就存在多语言本地化的问题,然后直接用过一个通过xml完成本地化的东东,然后策划反馈不会修改xml,扔给我一个excel让我自己 ...

  6. 开源业务规则引擎JBoss Drools

    Drools 是什么? 规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规 ...

  7. [Windows Server 2012] 安装护卫神·主机管理系统

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装护卫神·主 ...

  8. jsp 文件下载

    有的时候一个模板的下载,这种简单的下载服务端已存在文件功能,就可以方便的通过jsp文件下载的方式来轻松实现. //jsp 页面 js /** * 导出角色 */ function exportRole ...

  9. windows server2003 多用户登陆问题解决办法

    windows server2003 多用户登陆问题解决办法 Windows Server远程登陆默认情况下只允许同时有两个用户登陆,超过两个用户会提示"超出最大连接数". 要解决 ...

  10. JQuery文档加载完成执行js的几种方法

    js中文档加载完毕.一般在body加一个onload事件或者window.onload = function () {} jQuery中有好多写法,平时也不注意,别人一问,还真觉得头大. 下面是我整理 ...