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 ...
随机推荐
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 3 The law of averages, and expected values
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- AngularJs $q 承诺与延迟
$q 一个帮助处理异步执行函数的服务.当他们做完处理时,使用它们的返回值(或异常). 受 Kris Kowa’s Q 的启发,这是一个实现promise/deferred对象的启用. $q的两种方式- ...
- 【Alpha阶段】第一次线上会议
会议信息 因编译作业ddl,暂时没有大进展,没有close的issue 时间:2016.11.07 19:00 时长:10min 地点:讨论组 类型:线上会议 NXT:2016.11.08 21:30 ...
- HTML5学习总结-10 Android 控件WebView显示网页
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient 1)setWebClient: ...
- BZOJ2286: [Sdoi2011]消耗战
建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...
- spring-data-jpa Repository的基本知识
1.项目中的Repository对象的使用 2.Repository 引入的两种方式 继承和使用注解 3.Repository接口的定义 Repository 接口是 spring Data 的一个核 ...
- SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法
今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...
- WinForm------PopupMenu控件的使用
转载: http://www.cnblogs.com/xlx0210/archive/2010/07/14/1777366.html
- CURL函数的GET和POST方式的两种写法(实现ajax跨域调用)
POST请求 function curl_post($url='',$postdata='',$options=array()){ $ch=curl_init($url); curl_setopt($ ...
- JavaScript学习笔记——BOM_window子对象_History、Location、Screnn对象
javascript-History.Location.Screnn对象实例讲解 一.history对象 包含浏览器访问过的url 1.属性 length 返回浏览器历史记录的数量 alert(his ...