题目大意:n个高楼,每个楼最高为mi,要求,第i个楼左边和右边不能有同时比它高的楼。让你求最在n个楼总和最高的情况下,每个楼的高度。

题解:用单调栈来做,n个楼的高度要么是单调递减,要么是单调递增,要么就是先曾后减,就这3种情况,其他的不可能。

维护一个单调非递减的栈,并且维护一个数组ans[],第i个位置,维护的是i左边的所有楼的最大高度和。

当新元素比栈顶元素大时或者直接ans[i]=m[i]+ans[i-1]。当新元素比栈顶元素小时,一直出栈,直到栈为空(ans[i]=arr[i]*i),或者找到一个比新元素小于等于设为top的,也就是说top到i之间的元素都要比i元素大

所以ans[i]=ans[i-1]+arr[i]*(i-top)。然后数组逆序在来一遍。

遍历一遍数组,拼接左端与右端,找到和最大时的峰值。然后......

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=5E5+;
ll arr[N];
ll lans[N];
ll rans[N];
ll n;
void solve(ll ans []){
stack<ll >st;
ans[]=;
for(ll i=;i<=n;i++){
if(st.empty()) {
st.push(i);
ans[i]=arr[i]+ans[i-];
}
else {
ll top=st.top();
while(st.size()&&arr[i]<arr[top]){
st.pop();
if(st.empty()) {
break;
}
top=st.top();
}
if(st.empty()) ans[i]=i*arr[i];
else {
top=st.top();
ans[i]=arr[i]*(i-top)+ans[top];
}
st.push(i);
}
}
}
int main()
{ cin>>n;
for(ll i=;i<=n;i++) cin>>arr[i];
solve(lans);
reverse(arr+,arr++n);
solve(rans);
ll ans=,pos;
for(ll i=;i<=n;i++){
ll c=lans[i]+rans[n-i];
if(c>=ans) {
ans=c;
pos=i;
}
}
reverse(arr+,arr++n);
for(ll i=pos-;i>=;i--){
if(arr[i]>arr[i+]) arr[i]=arr[i+];
}
for(ll i=pos+;i<=n;i++) {
if(arr[i]>arr[i-]) arr[i]=arr[i-];
}
for(ll i=;i<=n;i++) cout<<arr[i]<<" "; return ;
}

X - Skyscrapers (hard version) CodeForces - 1313C2的更多相关文章

  1. Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)

    Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 题意: 你是一名建筑工程师,现给出 n 幢建筑的预计建设高度,你想建成峰状, ...

  2. Codeforces Round #622(Div 2)C2. Skyscrapers (hard version)

    题目链接 : C2. Skyscrapers (hard version) 题目描述 : 与上一道题类似,只是数据范围变大, 5e5, 如果用我们原来的方法,铁定是超时的. 考察点 : 单调栈,贪心, ...

  3. Codeforces Round #622(Div 2) C1. Skyscrapers (easy version)

    题目链接: C1. Skyscrapers (easy version) 题目描述: 有一行数,使得整个序列满足 先递增在递减(或者只递增,或者只递减) ,每个位置上的数可以改变,但是最大不能超过原来 ...

  4. Codeforces Round #622 C2.Skyscrapers (hard version)

    This is a harder version of the problem. In this version n≤500000n≤500000 The outskirts of the capit ...

  5. Codeforces Round #622 (Div. 2) C1. Skyscrapers (easy version)(简单版本暴力)

    This is an easier version of the problem. In this version n≤1000n≤1000 The outskirts of the capital ...

  6. Codeforces Round #622 (Div. 2).C2 - Skyscrapers (hard version)

    第二次写题解,请多多指教! http://codeforces.com/contest/1313/problem/C2 题目链接 不同于简单版本的暴力法,这个数据范围扩充到了五十万.所以考虑用单调栈的 ...

  7. Codeforces Round #622 (Div. 2) C2 - Skyscrapers (hard version) 单调栈

    从左往右扫,找到比第i个小的第一个数字,l[i] = l[last] + (i - last) * m[i],用单调栈O(n)维护这个过程,再从右往左扫,同理可以算出r数组,注意一下long long ...

  8. D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)

    D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...

  9. Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)

    The only difference between easy and hard versions is a number of elements in the array. You are giv ...

随机推荐

  1. 【codeforces】Codeforces Round #612 (Div. 2) C. Garland——DP

    题目链接 贪心模拟了半天,最后放弃了 题意 给你一串从1−n1-n1−n的序列,其中部分未知(表示为0),补全序列使得相邻数值奇偶性相反的数量最少 相邻数值的奇偶性相反:两个相邻的两个数值,其中一个为 ...

  2. 2、Spark Core职责之初始化(1)——SparkContext

    SparkContext(Spark上下文) /** * Main entry point for Spark functionality. A SparkContext represents the ...

  3. MATLAB 颜色图函数(imagesc/scatter/polarPcolor/pcolor)

    2维的热度图 imagesc imagesc(x, y, z),x和y分别是横纵坐标,z为值,表示颜色 imagesc(theta,phi,slc); colorbar xlabel(); ylabe ...

  4. Hive常用命令及作用

    1-创建表 -- 内部表 create table aa(col1 string,col2 int) partitioned by(statdate int) ROW FORMAT DELIMITED ...

  5. vue cli3配置开发环境、测试环境、生产(线上)环境

    cli3创建vue项目是精简版的少了build和config这2个文件,所以配置开发环境.测试环境.生产环境的话需要自己创建env文件. 需要注意2点: 1.cli2创建项目生成的config文件里的 ...

  6. WordPress 迁移站点更换域名为新域名

    使用 wp-cli 工具搜索替换域名的方式更换 WordPress 域名 wp-cli 是一个命令行工具,可以让我们通过命令行安装.更新 WordPress,对 WordPress 执行一些批量操作, ...

  7. matplotlib.pyplot.text

    matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=<deprecated parameter>, **kwargs)[sour ...

  8. CSS常用属性之选择器

    css选择器 序号 选择器 例子 例子描述 1 .class .intro 选择class="intro"的所有元素 2 #id #firstname 选择id="fir ...

  9. iOS 应用签名

    一.密码学简介 1.1 base64 Base64 是一种通过查表的编码方法,不能用于加密,即使使用自定义的编码表也不行. Base64 适用于小段内容的编码,比如数字证书签名.Cookie 的内容等 ...

  10. centos7中安装redis

    http://www.open-open.com/lib/view/open1426468117367.html https://www.cnblogs.com/cndavidwang/p/64294 ...