CF #305(Div.2) D. Mike and Feet(数学推导)
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 × 10^5), the number of bears.
The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 10^9), 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
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int M = 2e5 + ;
int l[M] , r[M] , a[M] ;
int b[M] ;
int n ;
int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &n) ;
for (int i = ; i <= n ; i ++) scanf ("%d" , &a[i]) ;
a[] = ; a[n + ] = ;
memset (b , - , sizeof(b)) ;
for (int i = ; i <= n ; i ++) {
l[i] = i ;
while (a[l[i] - ] >= a[i]) l[i] = l[l[i] - ] ;
}
for (int i = n ; i >= ; i --) {
r[i] = i ;
while (a[r[i] + ] >= a[i]) r[i] = r[r[i] + ] ;
}
for (int i = ; i <= n ; i ++) {
int id = r[i] - l[i] + ;
b[id] = std::max (b[id] , a[i]);
}
for (int i = n ; i >= ; i --) {
if (b[i - ] < b[i] ) b[i - ] = b[i] ;
}
for (int i = ; i <= n ; i ++) printf ("%d%c" , b[i] , i == n ? '\n' : ' ') ;
return ;
}
/*10
1 2 3 4 5 4 3 2 1 6
*/
- 最质补的想法,我们先考虑a[i]能影响的区间大小为w[i],(另b[i] 为长度为i的区间的最小的最大值)
for i = 1 to n
for j = 1 to w[i]
b[j] = max (b[j] , a[i]);
end
end
这样最坏情况下O(n^n),显然tle.
通过yy可以发现,区间大的可以跟新区间小的,so:
for i = 1 to n
b[w[i]] = max (b[w[i]] , a[i]);
end
for i = n to 2
if (b[i - 1] < b[i] ) b[i - 1] = b[i] ;
end
这样就变为O(n)了。
2.第二个要解决的问题时我们怎样在O(n)内求出w[i] ?
令w[i] = r[i] - l[i] ;
令r[i] , l[i]分别保存a[i]所能影响大的最右 和 最左。
这样在已知l[i]时,我们便可以在O(1)时间内求出l[i + 1]了。
CF #305(Div.2) D. Mike and Feet(数学推导)的更多相关文章
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #305 (Div. 1) B. Mike and Feet
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
随机推荐
- Some Simple Models of Neurons
Linear neuron: \[y=b+\sum\limits_i{x_i w_i}\] Binary threshold neuron: \[z = \sum\limits_i{x_i w_i}\ ...
- linux 基础 shell脚本命令
#########shell脚本命令#### 1.diff diff file file1 ####比较两个文件的不同 -c ####显示周围的行 -u ####按照一格式统一输出生成补丁 -r ## ...
- POJ2318TOYS(叉积判断点与直线位置)
题目链接 题意:一个矩形被分成了n + 1块,然后给出m个点,求每个点会落在哪一块中,输出每块的点的个数 就是判断 点与直线的位置,点在直线的逆时针方向叉积 < 0,点在直线的顺时针方向叉积 & ...
- FIREFOX A tool for easily making HTTP requests (GET/PUT/POST/DELETE)
https://addons.mozilla.org/zh-CN/firefox/addon/httprequester/
- 天行API服务器地址申请
http://www.tianapi.com/ http://www.huceo.com/post/383.html
- AspectJ基础学习之一简介(转载)
AspectJ基础学习之一简介(转载) 一.为什么写这个系列的博客 Aspectj一个易用的.功能强大的aop编程语言.其官网地址是:http://www.eclipse.org/aspectj/ ...
- JSP 入门 HTML嵌套Java脚步 显示时间
<%@ page import="java.util.Date"%> <%@ page language="java" contentType ...
- Latex论文写作-Texsdudio 快捷键总结
Latex论文写作-Texsdudio 快捷键总结 The keyboard shortcuts can be modified at Options -> Shortcuts. The fo ...
- Nginx配置优化的几个参数
worker_processes 8 一般CPU(i/o)密集型配置为核数相同,网络(i/o)密集型配置为核数倍数(我配置为2倍) worker_cpu_affinity(这个没用过) 仅适用于lin ...
- Docker Dockerfile COPY vs ADD
http://blog.163.com/digoal@126/blog/static/163877040201410341236664/ 在Dockerfile中, 我们可以使用ADD和COPY拷 ...