Codeforces Round #305 (Div. 2) D 维护单调栈
1 second
256 megabytes
standard input
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 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.
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.
Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.
10
1 2 3 4 5 4 3 2 1 6
6 4 4 3 3 2 2 1 1 1
题意:给你一个长度为n的数列 求长度为x x取值(1,n) 的区段最小值的最大值
题解:求以a[i]为最小值的区段的左界右界;
dp[r[i]-l[i]+1]=max(dp[r[i]-l[i]+1],a[i]) 倒序取max得到每个长度的答案;
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define ll long long
#define mod 1000000007
using namespace std;
int n;
int a[];
int l[];
int r[];
int ans[];
int dp[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
a[]=-;
a[n+]=-;
l[]=;
for(int i=; i<=n; i++) //关键********
{
int temp=i-;
while(a[temp]>=a[i])//维护一个递增的序列
temp=l[temp]-;
l[i]=temp+;
}
r[n]=n;
for (int i=n-; i>=; i--)
{
int temp=i+;
while(a[temp]>=a[i])
temp=r[temp]+;
r[i]=temp-;
}
for(int i=;i<=n;i++)
dp[r[i]-l[i]+]=max(dp[r[i]-l[i]+],a[i]);
int res=;
for(int i=n;i>=;i--){
res=max(res,dp[i]);
ans[i]=res;
}
for(int i=;i<=n;i++)
printf("%d ",ans[i]);
return ;
}
最新文章
- 昨天写支付接口时遇到支付接口返回数据接收地址,session数据丢失(或者说失效)的问题
- Autoit3 获取WinForm下的ToolTip
- JNI系列——PassData
- flash builder4.7bug
- 调整iFrame高度
- sql必知必会(第四版) 学习笔记
- Ubuntu上安装Maven Eclipse以及配置
- Ext.Ajax.request
- Java异常处理示例
- Batch Normalization原理
- Java开发之@PostConstruct执行顺序
- highcharts echarts比较
- [k8s]kube-dns架构图解
- mysql 事件 按月分表
- ---转载---phython资料
- LintCode: Single Number II
- wprintf或_tprintf不显示中文和乱码问题
- svn错误:Can't convert string from 'UTF-8' to native encoding
- eclipse逆向生成实体类注解方式或者xml方式
- gulp#4.0 Did you forget to signal async completion?