Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)
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 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.
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 先用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)的更多相关文章
- 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 ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Java并发编程系列之Semaphore详解
简单介绍 我们以饭店为例,假设饭店只有三个座位,一开始三个座位都是空的.这时如果同时来了三个客人,服务员人允许他们进去用餐,然后对外说暂无座位.后来的客人必须在门口等待,直到有客人离开.这时,如果有一 ...
- JavaScript入门二
******函数****** **函数定义** //普通函数定义 function f1() { console.log("Hello word!") } //带参数的函数 fun ...
- Croppic插件使用介绍-asp.net
具体的参数使用和基本使用方式请看:http://www.uedsc.com/croppic-api.html 需要说明的几点: 1.支持两种上传方式: (1)先将原图上传至服务器,然后再次将切图信息传 ...
- InputStream和Reader
java.io下面有两个抽象类:InputStream和ReaderInputStream是表示字节输入流的所有类的超类Reader是用于读取字符流的抽象类InputStream提供的是字节流的读取, ...
- python--11、协程
协程,又称微线程,纤程.英文名Coroutine. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执行过程中又调用了C,C执行完毕返回,B执行完毕返回,最后是A执行完毕. 所以子程 ...
- Android ScrollView里嵌套RecyclerView时,在RecyclerView上滑动时出现卡顿(冲突)的现象
最近在项目中遇到一个现象,一个界面有一个RecyclerView(GridView型的),外面套了一层ScrollView,通过ScrollView上下滚动,但是在滑动的时候如果是在RecyclerV ...
- Redux 基础概念
Redux is a predictable state container for JavaScript apps.,亦即 Redux 希望能提供一个可以预测的 state 管理容器,让开发者可以可 ...
- Android Studio 快捷键整理
Alt+回车 导入包,自动修正 Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如ge ...
- spring 415
不支持的媒体类型 spring mvc 使用@requestBody注解json请求时,jQuery有限制,否则会出现 415 错误 1.使用ajax $.ajax({ ...
- js 性能调试
今天有幸偶遇我早就神往已久的性能调试问题. 原来js调试工具里面有可以记录每个方法的执行时间的功能,站在此功能的肩膀上就可以对自己的程序性能.瓶颈了如指掌,就可以针对性的,瞄准目标,斩草除根,以绝后患 ...