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
  1. 10
    1 2 3 4 5 4 3 2 1 6
output
  1. 6 4 4 3 3 2 2 1 1 1
  2.  
  3. 先用ST算法处理出一段区间内最小值。二分查询出对于第i个数,以它为最小向左或向右可达的最值,得到区间长度为r-l+1。那么,对于group长度为123,....r-l+1最小值为第i个数的值。则区间更新这些组大小,取最大值。此处可以使用线段树维护。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6. const int N=200050;
  7. const int inf=1e9+107;
  8.  
  9. int feet[N];
  10. int st[N][20];
  11. int seg[N<<2];
  12. int ans[N];
  13. void pushdown(int rt){
  14. seg[rt<<1]=max(seg[rt<<1],seg[rt]);
  15. seg[rt<<1|1]=max(seg[rt<<1|1],seg[rt]);
  16. seg[rt]=-1;
  17. }
  18.  
  19. void ST(int num){
  20. for(int i=1;i<=num;i++)
  21. st[i][0]=feet[i];
  22. for(int j=1;j<20;j++)
  23. for(int i=1;i<=num;i++){
  24. if(i+(1<<j)-1 <= num){
  25. st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
  26. }
  27. }
  28. }
  29.  
  30. void update(int rt,int l,int r,int L,int R,int val){
  31. if(l<=L&&R<=r){
  32. if(val>seg[rt]) seg[rt]=val; return;
  33. }
  34. int m=(L+R)>>1;
  35. pushdown(rt);
  36. if(r<=m){
  37. update(rt<<1,l,r,L,m,val);
  38. }
  39. else if(l>=m+1){
  40. update(rt<<1|1,l,r,m+1,R,val);
  41. }
  42. else{
  43. update(rt<<1,l,r,L,m,val);
  44. update(rt<<1|1,l,r,m+1,R,val);
  45. }
  46. }
  47.  
  48. void dfs(int rt,int L,int R){
  49. if(L==R){
  50. ans[L]=seg[rt];
  51. return ;
  52. }
  53. pushdown(rt);
  54. int m=(L+R)>>1;
  55. dfs(rt<<1,L,m);
  56. dfs(rt<<1|1,m+1,R);
  57. }
  58.  
  59. int query(int l,int r){
  60. int k=(int)((log(r-l+1))/log(2.0));
  61. int maxl=min(st[l][k],st[r-(1<<k)+1][k]);
  62. return maxl;
  63. }
  64.  
  65. int main(){
  66. int n;
  67. while(scanf("%d",&n)!=EOF){
  68. for(int i=1;i<=(n<<2)+10;i++){
  69. seg[i]=-1;
  70. }
  71. for(int i=1;i<=n;i++){
  72. scanf("%d",&feet[i]);
  73. }
  74. ST(n);
  75. for(int i=1;i<=n;i++){
  76. //left
  77. int l=1,r=i;
  78. int L=i,R=i;
  79. while(l<=r){
  80. int m=(l+r)>>1;
  81. int index=query(m,i);
  82. if(index>=feet[i]){
  83. r=m-1;
  84. L=m;
  85. }
  86. else {
  87. l=m+1;
  88. }
  89. }
  90. //right
  91. l=i,r=n;
  92. while(l<=r){
  93. int m=(l+r)>>1;
  94. int index=query(i,m);
  95. if(index>=feet[i]){
  96. l=m+1;
  97. R=m;
  98. }
  99. else {
  100. r=m-1;
  101. }
  102. }
  103. update(1,1,R-L+1,1,n,feet[i]);
  104. }
  105. dfs(1,1,n);
  106. printf("%d",ans[1]);
  107. for(int i=2;i<=n;i++)
  108. printf(" %d",ans[i]);
  109. printf("\n");
  110. }
  111. return 0;
  112. }

  

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. Appium + python - TouchAction操作

    from appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionfrom appium. ...

  2. Git学习之序

    最近在忙毕业论文的事,需要用NS2仿真,其中需要修改NS2的源码,故想藉此机会学习一下Git,方便代码的管理. 由于我在以前实习的时候接触过代码管理工具SVN,因此对代码管理的一些概念还是有的.如果从 ...

  3. Java中的overload(方法的覆写)

    方法覆写(overload)与方法的重载非常相似,它在 Java的继承中也有很重要的应用. 写程序可能会碰到下面的情况,在父类中已经实现的方法可能不够精确,不能满足子类 的需求.例如在前面的 Anim ...

  4. hibernate annotation 之 一对多、多对一双向外键关联

    假设,一个农场产出多种植物,具体的某一植物产于某一农场. 3 import java.io.Serializable; 4 import java.util.Set; 5 import javax.p ...

  5. (转)44 道 JavaScript 难题

    JavaScript Puzzlers原文 1. ["1", "2", "3"].map(parseInt)   答案:[1, NaN, N ...

  6. SAS进阶《深入解析SAS》之Base SAS基础、读取外部数据到SAS数据集

    SAS进阶<深入解析SAS>之Base SAS基础.读取外部数据到SAS数据集 前言:在学习完<SAS编程与商业案例>后,虽然能够接手公司的基本工作,但是为了更深入的SAS学习 ...

  7. Python3之切片的道理

    list的切片有三个参数:起点,终点,步长 list[::-1] 相当于起点为最后的一个,终点为第一个,然后一次减少一个 更多的看下面的测试 >>> a = [0,1,2,3,4,5 ...

  8. printFinal用法示例

    printFinal是一个基于jQuery的打印插件,支持打印预览,使用很简单,废话不多多说,直接上代码. <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  9. Ubuntu用户自定义脚本开机启动

    如果想让自己写的脚本随开机自动执行,可以这样做: 编辑文件/etc/init.d/rc.local,在最后添加用户自定义脚本的完整路径即可. 很简单有木有!!

  10. R语言图表

    条形图 在R语言中创建条形图的基本语法是 barplot(H, xlab, ylab, main, names.arg, col) H是包含在条形图中使用的数值的向量或矩阵 xlab是x轴的标签 yl ...