前几天做的题,当时好像是超时了,这个博客写的超好https://blog.csdn.net/lucky52529/article/details/89155694

用单调站解决问题。

代码是从另外一篇博客来的,谢谢了,贴过来,如果介意的话我就删掉

https://blog.csdn.net/weixin_44164153/article/details/104486676?fps=1&locationNum=2

#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define INF 0x3f3f3f3f const int maxn = 5e5+;
int n,a[maxn],l[maxn],r[maxn],res[maxn];
signed main() {
IO;
cin >> n;
for (int i = ; i <= n; i++) cin >> a[i];
vector<int> q;
q.push_back();
for (int i = ; i <= n; i++) {
while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();
int t = q.back();
l[i] = l[t] + a[i] * (i - t);
q.push_back(i);
}
q.clear();
q.push_back(n+);
for (int i = n; i >= ; i--) {
while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();
int t = q.back();
r[i] = r[t] + a[i] * (t - i);
q.push_back(i);
}
int ans = , pos = ;
for (int i = ; i <= n; i++) {
int temp = l[i] + r[i] - a[i];
if (temp > ans) {
ans = temp, pos = i;
}
}
res[pos] = a[pos];
for (int i = pos - ; i >= ; i--) res[i] = min(a[i], res[i + ]);
for (int i = pos + ; i <= n; i++) res[i] = min(a[i], res[i - ]);
for (int i = ; i <= n; i++) cout << res[i] << " ";
cout << endl;
return ;
}

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

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

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

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

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

  3. 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 ...

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

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

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

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

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

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

  7. 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 ...

  8. Codeforces Round #622 (Div. 2)C2 Skyscrapers最大"尖"性矩形,思维||分治

    题:https://codeforces.com/contest/1313/problem/C2 题意:给出n个数,分别代表第i个位置所能搭建的最大高度,问以哪一个位置的塔的高度为基准向左的每一个塔都 ...

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

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

随机推荐

  1. bootstrap标记说明

    <span class="caret"> 这就是 一个倒三角

  2. 树莓派4B踩坑指南 - (9)安装Git和Docker

    安装Git sudo apt-get install wget git-core 安装Docker curl -sSL https://get.docker.com | sh # 树莓派专属脚本福利, ...

  3. locust --hellp

    1. Locust简介 Locust是使用Python语言编写实现的开源性能测试工具,简洁.轻量.高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力. 官网:https://loc ...

  4. 并发队列之ArrayBlockingQueue

    上一篇我们说了并发队列中的LinkedBlockingQueue队列,这次我们看看ArrayBlockingQueue,看看名字,我们想象一下LinkedList和ArrayList的区别,我们可以知 ...

  5. Mockito 中文文档 ( 2.0.26 beta )

    Mockito 中文文档 ( 2.0.26 beta ) 由于缺乏校对,难免有谬误之处,如果发现任何语句不通顺.翻译错误,都可以在github中的项目提出issue.谢谢~ Mockito框架官方地址 ...

  6. 虚拟机与ubuntu系统的安装与基础操作

    1.虚拟机的下载: 常见的虚拟机软件有:VMware  VirtuaIBOX  Virtual PC  等. 这里主要介绍VMware ,VMware目前已经有很多个版本,可以根据个人情况进行选择.安 ...

  7. Navigating to current location ("/") is not allowed

    main.js import Router from 'vue-router' // 这个是为了避免一个报错 const originalPush = Router.prototype.push; R ...

  8. 名称空间using namespace std

    写再最前面:摘录于柳神的笔记 虽然C++是一门面向对象语言,但是对于刷算法这件事而言,我们并不需要掌握它⾯向对象的部分-只 需要掌握刷算法的时候需要用到的部分(基本输⼊输出.STL标准模板库. str ...

  9. 3_07_MSSQL课程_Ado.Net_委托、事件

    委托和事件的区别? (1)委托是一个类. 事件是一个委托类型的实列. (2)委托可以在定义委托实列的类的外部触发执行.(不安全) 事件只能能在定义它的类的内部触发执行.(安全),类外部只能注册事件的响 ...

  10. The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)

    题意:给你一个长度为$n$的数组,定义函数$f(l,r)=a_{l} \oplus a_{l+1} \oplus...\oplus a_{r}$,$F(l,r)=f(l,l)\oplus f(l,l+ ...