CodeForces 548D 单调栈
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
Sample Input
10
1 2 3 4 5 4 3 2 1 6
/*
* Author: sweat123
* Created Time: 2016/7/12 10:39:09
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN],n,ans[MAXN];
struct node{
int len;
int val;
node(){}
node(int tlen,int tval):len(tlen),val(tval){}
};
stack<node>s;
int main(){
while(~scanf("%d",&n)){
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
while(!s.empty())s.pop();
memset(ans,,sizeof(ans));
for(int i = ; i <= n; i++){
int len = ;//表示当前弹出的这些元素能够贡献多少的长度
while(!s.empty()){
node tp = s.top();
if(tp.val < a[i])break;
s.pop();
int ret = tp.len + len;//这个元素能够贡献的长度 (也就是说这个元素已经统计过了多少长度,在这里也是可以连续的)
ans[ret] = max(ans[ret],tp.val);
len += tp.len;
}
s.push(node(len+,a[i]));
}
int len = ;
while(!s.empty()){
node tp = s.top();
s.pop();
int ret = len + tp.len;
ans[ret] = max(ans[ret],tp.val);
len += tp.len;
}
for(int i = n; i >= ; i--){
ans[i] = max(ans[i],ans[i+]);
}
for(int i = ; i <= n; i++){
printf("%d ",ans[i]);
}
printf("\n");
}
return ;
}
CodeForces 548D 单调栈的更多相关文章
- 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 ...
- Mike and Feet CodeForces - 548D (单调栈)
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
- Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- 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 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- codeforces 817 D. Imbalanced Array(单调栈+思维)
题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...
- Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...
随机推荐
- JS判断用户手机是IOS还是Android
$(function () { var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf(' ...
- iPad编程
1. iPad 现有型号: iPad Pro, iPad Air, iPad mini 均配备Retina显示屏.早期还有iPad 依次对应的坐标系及分辨率: iPad Pro 坐标系:1366 x ...
- NSError
-(NSString * )backErrorString{ // self.code; //http://blog.csdn.net/linkai5696/article/details/59 ...
- 课程上线 -“新手入门 : Windows Phone 8.1 开发”
经过近1个月的准备和录制,“新手入门 : Windows Phone 8.1 开发”系列课程已经在Microsoft 虚拟学院上线,链接地址为:http://www.microsoftvirtuala ...
- W3School-CSS 列表实例
CSS 列表实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CSS 内边距 (paddi ...
- android 项目中出现红色感叹号的解决方法
问题原因]:工程中classpath中指向的包路径错误 [解决办法]:右键项目名称 BuildPath ---> Configure Build Paht...中,然后上面有几个选项卡找到 Li ...
- Shell编程和Vim操作
其实一直不懂什么是shell,安卓adb调试时会使用一些简单的shell命令,总结一下 1.adb调试命令 全称:Android Debug Bridge 设置: export PATH=${PATH ...
- java.lang.Class.forName(String name, boolean initialize, ClassLoader loader)方法
描述 Java.lang.Class.forName(String name, boolean initialize, ClassLoader loader) 方法返回与给定字符串名的类或接口的Cla ...
- GCC 中零长数组与变长数组
前两天看程序,发现在某个函数中有下面这段程序: int n; //define a variable n int array[n]; //define an array with length n 在 ...
- Django博客功能实现—文章评论功能
功能:在A网页提交一个评论Forms_B,提交之后自动刷新页面,能够显示刚刚的画面思路:利用一个已经创建的表单,通过视图让其在网页中表现出来,填写玩信息之后提交,会提交到一个新的视图里面去做接受,接受 ...