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 strength of 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.

Examples

Input

10

1 2 3 4 5 4 3 2 1 6

Output

6 4 4 3 3 2 2 1 1 1

思路:

我们可以用单调栈O(N)维护出每一个数在数字中左边第一个比它大的坐标和右边第一个比它大的坐标。

那么每一个数a[i] 在数组中为最小值的最大区间就可以求出,

然后我们可以计算每一个数a[i] 对答案的贡献,

那么肯定还有一些区间长度没有被更新到,

可以根据ans 数组的定义来获得,

如果 没有一个数a[i] 为最小值的区间长度为3,而又一个数a[j] 为最小值的区间长度为4

那么ans[3] 就可以等于 ans[4] ,因为 a[j] 长度为4的区间 去掉开头或者末尾任意一个位置,就可以得到长度为3的区间。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
stack<int> st;
int l[maxn];
int r[maxn];
int a[maxn];
int n;
void solveinfo()
{
a[0]=a[n+1]=-inf;
while(st.size())
{
st.pop();
}
repd(i,0,n+1)
{
while(st.size()&&a[i]<=a[st.top()])
{
st.pop();
}
if(st.size())
{
l[i]=st.top();
}else
{
l[i]=-1;
}
st.push(i);
}
while(st.size())
{
st.pop();
}
for(int i=n+1;i>=1;--i)
{
while(st.size()&&a[i]<=a[st.top()])
{
st.pop();
}
if(st.size())
{
r[i]=st.top();
}else
{
r[i]=-1;
}
st.push(i);
}
}
int ans[maxn];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>n;
repd(i,1,n)
{
cin>>a[i];
}
solveinfo();
MS0(ans);
repd(i,1,n)
{
int len=r[i]-l[i]-1;
// cout<<i<<" "<<r[i]<<" "<<l[i]<<" "<<" "<<len<<endl;
ans[len]=max(ans[len],a[i]);
}
for(int i=n-1;i>=1;--i)
{
ans[i]=max(ans[i],ans[i+1]);
}
repd(i,1,n)
{
printf("%d%c",ans[i],i==n?'\n':' ');
}
return 0; } inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Mike and Feet CodeForces - 548D (单调栈)的更多相关文章

  1. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  2. Discrete Centrifugal Jumps CodeForces - 1407D 单调栈+dp

    题意: 给你n个数hi,你刚开始在第1个数的位置,你需要跳到第n个数的位置. 1.对于i.j(i<j) 如果满足 max(hi+1,-,hj−1)<min(hi,hj) max(hi,hj ...

  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(单调栈)

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

  5. codeforces 547B. Mike and Feet 单调栈

    题目链接 用单调栈计算出一个数字, 左边第一个比他小的数字的位置, 右边比第一个他小的数字的位置, 然后len = r[i] - l[i] +1. ans[len] = max(ans[len], a ...

  6. 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 ...

  7. Codeforces 547B. Mike and Feet[单调栈/队列]

    这道题用单调递增的单调栈维护每个数能够覆盖的最大区间即可. 对于   1 2 3 4 5 4 3 2 1 6 这组样例, 1能够覆盖的最大区间是10,2能够覆盖的最大区间是7,以此类推,我们可以使用单 ...

  8. Codeforces548D:Mike and Feet(单调栈)

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

  9. CF Mike and Feet (求连续区间内长度为i的最小值)单调栈

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

随机推荐

  1. 5种Redis数据结构详解

    本文主要和大家分享 5种Redis数据结构详解,希望文中的案例和代码,能帮助到大家. 转载链接:https://www.php.cn/php-weizijiaocheng-388126.html 2. ...

  2. web赛题3

    2019--21省赛 wp:https://xz.aliyun.com/t/6458 2019-11-22蚂蚁金服(南邮)wp有了,微信 https://platform.d3ctf.io/#/ @d ...

  3. 【C/C++开发】C语言 DLL(动态链接库)中申请动态内存释放的问题

    参考:首先,声明一点,凡是使用malloc之类命令动态申请的内存,必须进行释放操作,否则就会发生内存泄漏问题. DLL中申请的内存释放,如果没有做过,很可能会认为是直接在调用程序中释放就可以了,其实不 ...

  4. C++学习笔记-异常处理

    程序设计的要求之一就是程序的健壮性.希望程序在运行时能够不出或者少出问题.但是,在程序的实际运行时,总会有一些因素会导致程序不能正常运行.异常处理(Exception Handling)就是要提出或者 ...

  5. 截取铃声python代码

    from pydub import AudioSegment file_name = "张杰 - 这就是爱.mp3" sound = AudioSegment.from_mp3(f ...

  6. SpringBoot中使用aop-测试

    面向切面编程(AOP),该种方式主要是为了弥补面向对象编程(OOP)的不足,通过配置切面以及关注点.通知等我们可以在程序的任意位置对我们的代码进行增强(执行一些代码),AOP是Spring的特性之一, ...

  7. Centos7 下安装docker

    Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上. Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位.系统内核 ...

  8. 【Python】【demo实验5】【练习实例】【多个数字组合成不重复三位数】

    题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源代码 ...

  9. GIP画图

    世界坐标:相对于winform窗体来说的, 页面坐标:相对于控件的 设置坐标:相对于显示器 获得Graphics对象一般有两种方式: 1.控件.CreateGraphics();//通过该方式创建后要 ...

  10. spring-boot 使用 jackson 出错(五)

    环境 jdk 6 tomcat 6.0.53 sts 4.4.2 maven 3.2.5 原因 spring boot 1.5.22.RELEASE 默认使用的 jackson 的版本是 2.8.x, ...