思路:

使用单调栈。

实现:

 1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int N = 500005;
5 ll a[N], l[N], r[N];
6 int main()
7 {
8 int n;
9 while (cin >> n)
10 {
11 stack<int> st;
12 for (int i = 1; i <= n; i++) cin >> a[i];
13 for (int i = 1; i <= n; i++)
14 {
15 while (!st.empty() && a[st.top()] >= a[i]) st.pop();
16 if (st.empty()) l[i] = i * a[i];
17 else l[i] = l[st.top()] + (i - st.top()) * a[i];
18 st.push(i);
19 }
20 while (!st.empty()) st.pop();
21 for (int i = n; i >= 1; i--)
22 {
23 while (!st.empty() && a[st.top()] >= a[i]) st.pop();
24 if (st.empty()) r[i] = (n - i + 1) * a[i];
25 else r[i] = r[st.top()] + (st.top() - i) * a[i];
26 st.push(i);
27 }
28 ll res = 0, id = 0;
29 for (int i = 1; i <= n; i++)
30 {
31 ll tmp = l[i] + r[i] - a[i];
32 if (tmp > res) { res = tmp; id = i; }
33 }
34 for (int i = id - 1; i >= 1; i--) a[i] = min(a[i], a[i + 1]);
35 for (int i = id + 1; i <= n; i++) a[i] = min(a[i], a[i - 1]);
36 for (int i = 1; i <= n; i++) cout << a[i] << " ";
37 cout << endl;
38 }
39 return 0;
40 }

CF1313C2 Skyscrapers (hard version)的更多相关文章

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

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

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

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

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

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

  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) 单调栈

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

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

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

  8. C2 - Skyscrapers (hard version)

    前几天做的题,当时好像是超时了,这个博客写的超好https://blog.csdn.net/lucky52529/article/details/89155694 用单调站解决问题. 代码是从另外一篇 ...

  9. X - Skyscrapers (hard version) CodeForces - 1313C2

    题目大意:n个高楼,每个楼最高为mi,要求,第i个楼左边和右边不能有同时比它高的楼.让你求最在n个楼总和最高的情况下,每个楼的高度. 题解:用单调栈来做,n个楼的高度要么是单调递减,要么是单调递增,要 ...

随机推荐

  1. MySql中的有条件插入 insert where

    假设现在我们有这样的需求:当数据库中不存在满足条件的记录时,可以插入一条记录,否则程序退出.该怎么实现? 1年以上工作经验的人应该都能立即想到:去检查一下库里有没有记录,没有就插入,有就结束. int ...

  2. dbms_job和dbms_job基础学习

    一.dbms_job学习 a.创建job: dbms_job.submit(jobno,what,next_date,interval);b.删除job: dbms_job.remove(jobno) ...

  3. oracle 11.2.0.1.0 升级 11.2.0.4.0 并 patch 到11.2.0.4.7

    升级步骤: (1)    备份数据库 (2)    运行patchset,升级oracle 软件 (3)    准备新的ORACLE_HOME (4)    运行dbua 或者脚本升级实例 (5)   ...

  4. 几幅图,拿下 HTTPS

    我很早之前写过一篇关于 HTTP 和 HTTPS 的文章,但对于 HTTPS 介绍还不够详细,只讲了比较基础的部分,所以这次我们再来深入一下 HTTPS,用实战抓包的方式,带大家再来窥探一次 HTTP ...

  5. poj-Decoding Morse Sequences(动态规划)

    Description Before the digital age, the most common "binary" code for radio communication ...

  6. 为什么不建议用var

    看了这个例子估计你就会明白了 var a = 'global'; function test() { if (!a) { var a = 'part'; } console.log(a); } tes ...

  7. Gulp4.0入门和实战

    gulp4.0入门和实战 我最近遇到需要优化web的性能的任务,然后就捣鼓了一些对资源文件优化压缩的方案.由于之前的项目中有使用到gulp,所以在需要处理的web项目中也优先使用这个技术. 先聊聊gu ...

  8. StringBuilder和输入输出

    构建字符串(StringBuilder的应用) 有些时候,需要由较短的字符串构建字符串,例如:按键或来自文件的单词,采用字符串连接的方式达到此目的效率比较低.每次连接字符串,都会构建一个新的Strin ...

  9. 【Android初级】如何动态添加菜单项(附源码+避坑)

    我们平时在开发过程中,为了灵活多变,除了使用静态的菜单,还有动态添加菜单的需求.今天要分享的功能如下: 在界面的右上角有个更多选项,点开后,有两个子菜单:关于和退出 点击"关于", ...

  10. Vue基础之生命周期函数[残缺版]!

    Vue基础之生命周期函数[残缺版]! 为什么说是残缺版呢?! 因为有一些周期函数我并没有学到!所以是残缺版! 01 beforeCreate //在实例初始化之后,数据观测 (data observe ...