文章目录

  • [C2. Skyscrapers (hard version)](https://codeforces.com/contest/1313/problem/C2)
    • 题目大意
    • 解题思路
    • 代码

C2. Skyscrapers (hard version)

time limit per test:3 seconds memory limit per test:512 megabytes inputstandard input outputstandard output

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

The outskirts of the capital are being actively built up in Berland. The company “Kernel Panic” manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought n plots along the highway and is preparing to build n skyscrapers, one skyscraper per plot.

Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.

Formally, let’s number the plots from 1 to n. Then if the skyscraper on the i-th plot has ai floors, it must hold that ai is at most mi (1≤ai≤mi). Also there mustn’t be integers j and k such that j<iai<ak. Plots j and k are not required to be adjacent to i.

The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.

Input
The first line contains a single integer n (1≤n≤500000) — the number of plots.

The second line contains the integers m1,m2,…,mn (1≤mi≤109) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.

Output
Print n integers ai — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible.

If there are multiple answers possible, print any of them.

Examples
input

5
1 2 3 2 1

output

1 2 3 2 1

input

3
10 6 8

output

10 6 6

Note
In the first example, you can build all skyscrapers with the highest possible height.
In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer [10,6,6] is optimal. Note that the answer of [6,6,8] also satisfies all restrictions, but is not optimal.

题目大意

给你一个数字 n ,一共有 n 个数,你可以调整这串数字的大小,最后满足任何一个数字两边的数字都不大于它,调整的时候只能将一个数字调整的比他小;

解题思路

我们可以想象到这个答案只有三种形态,
1:单调非递增的 如:1 2 3 3 4 5 6
2:单调非递减的 如:6 5 4 3 3 2 1
3:中间某处凸起的,如:1 2 3 6 5 4 3
这样的话我们其实可以通过用单调栈从左开始按非递增的规则进行求一个前缀和;然后再从右开始用单调栈求一个后缀和,然后,再跑一遍看看以哪一个位置为最高点的结果最大;然后我们就可以按照这个点为最高点像两遍更新了;

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[500100],l[500100],r[500100];//l r 分别记录位置
ll suml[500100],sumr[500100];//用于求前缀和 int main ()
{ios::sync_with_stdio(0);int n; cin>>n;for(int i=1,j=0;i<=n;i++)//从左到右选 {cin>>a[i];j=i-1;while(j>0&&a[i]<a[j]) j=l[j];//单调栈进行筛选,如果满足单调性就放进去 l[i]=j;suml[i]=suml[j]+1LL*(i-j)*a[i];}ll s=0;int t;// t 用来记录最高点 for(int i=n,j=n+1;i>0;i--)//从右到左选 {j=i+1;while(j<=n&&a[i]<a[j]) j=r[j];r[i]=j;sumr[i]=sumr[j]+1LL*(j-i)*a[i];if(suml[i]+sumr[i]-a[i]>s)//选完以后,看看以哪个位置作为最高点比较好 {s=suml[i]+sumr[i]-a[i];t=i;}}for(int i=t-1;i>0;i--)//向左更新队列数据 {if(a[i]>a[i+1]) a[i]=a[i+1];}for(int i=t+1;i<=n;i++)//向右更新队列数据 {if(a[i]>a[i-1]) a[i]=a[i-1];}for(int i=1;i<=n;i++)//输出 cout<<a[i]<<" ";return 0;
}

C2. Skyscrapers (hard version)相关推荐

  1. [codeforces 1313C1] Skyscrapers (easy version) 问的是谷,答的是峰

    Codeforces Round #622 (Div. 2)   比赛人数5752 [codeforces 1313C1] Skyscrapers (easy version)   问的是谷,答的是峰 ...

  2. Skyscrapers (hard version) CodeForces - 1313C2(单调栈)

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

  3. Skyscrapers (easy version)CodeForces - 1313C1(暴力)

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

  4. CodeForces - 1313C2 Skyscrapers (hard version)(单调栈+dp/分治)

    题目链接:点击查看 题目大意:给出 n 块连续的空地可以建造摩天大楼,政府有规定,每块地最高只能建 a[ i ] 的高度,同时每栋大楼需要满足一个规则,即每栋大楼的两侧不允许同时存在比自己高的大楼,输 ...

  5. Codeforces Global Round 12 C2. Errich-Tac-Toe (Hard Version)(思维)

    题目描述 The only difference between the easy and hard versions is that tokens of type O do not appear i ...

  6. cf 723 C2. Potions (Hard Version)(反悔,priority)

    传送门 难过,就差一点点-- 大致翻译: 这是问题的硬版本.唯一的区别是在这个版本中≤20万.只有两个版本的问题都解决了,你才能进行黑客攻击. 一行有n种药剂,药剂1在最左边,药剂n在最右边.每一种药 ...

  7. Codeforces Round #708 (Div. 2)

    Codeforces Round #708 (Div. 2) 题号 题目 知识点 A Meximization 思维 B M-arrays 思维 C1 k-LCM (easy version) 构造 ...

  8. codeforces:ProblemMset

    最近一个月在codeforces上做的题(做个记录) 后面太多了就不把代码一一放出了,只放置了链接,可根据链接找到提交的代码. 最小子矩阵 #include <iostream> #inc ...

  9. 【Codeforces】Codeforces之丰【部分题解】

    Codeforces之丰 [by_041] 文章目录 Codeforces之丰 [by_041] @[toc] Codeforces Round #721 (Div. 2) A. And Then T ...

  10. 分布式共识算法随笔 —— 从 Quorum 到 Paxos

    分布式共识算法随笔 -- 从 Quorum 到 Paxos 概览: 为什么需要共识算法? 昨夜西风凋碧树,独上高楼,望尽天涯路 复制(Replication) 是一种通过将同一份数据在复制在多个服务器 ...

最新文章

  1. Spring AOP详解(转载)所需要的包
  2. 用 golang 1.11 module 做项目版本管理
  3. 我的世界服务器设置op显示,我的世界设置op权限 | 手游网游页游攻略大全
  4. 【自定义组件】如何引用自定义组件
  5. python-24: re 模块 之二 re方法及反斜杠
  6. Hyper-V 3.0网络虚拟化PART 4:私有交换机
  7. mysqli_connect参数的写法以及如何设置特定端口
  8. 当select查询为空
  9. LeetCode 985 Sum of Even Numbers After Queries 解题报告
  10. 奥维互动地图恢复旧版及导入谷歌卫星图
  11. 【跨境电商】WhatsApp营销保姆级教程!
  12. 一文带你吃透黑盒测试跟白盒测试的区别
  13. python数据分析——网络流量的一些特性
  14. 工行u盾显示316_工行U盾常见故障处理
  15. 适用于任何设备的屏幕共享应用程序 – Mirroring360
  16. 金立金刚GN5001刷机救黑砖
  17. 在cadence中使用VerilogA
  18. [附源码]计算机毕业设计Python+uniapp基于Android的自来水收费系统3e359(程序+源码+LW+远程部署)
  19. 量子计算机 中国科学院,中科院量子计算机取得重大突破
  20. 飞书机器人小助手@特定人

热门文章

  1. 倒残差与线性瓶颈浅析 - MobileNetV2
  2. 计算机技能比赛培训总结怎么写,技能大赛总结范文
  3. 【计算几何】求三角形外接圆的周长、面积公式
  4. K线形态识别—K线反转形态之缺口
  5. 数学竞赛书单该怎么选?金牌牛娃用的书都在这里了!
  6. oCPC实践录 | 成本优化策略之eCPC(3)
  7. 响应服务器530 5.7.0,SMTPSenderRefused(530,需要b'5.7.0身份验证)
  8. 英雄联盟英雄技能总结
  9. python运行出现OSError: [WinError 87] 参数错误。
  10. 什么是你的核心竞争力之二?