前几天做的题,当时好像是超时了,这个博客写的超好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. [经验] 如何将 Java 项目发布到云服务器上并可以访问

    环境: 云服务器 Linux centos7.6 1: 安装 Tomcat  (apache-tomcat-9.0.26.tar.gz) 下载压缩包 --> 通过SSH上传到云服务器 --> ...

  2. 固定ip配置

    1.打开 网络和internet设置 2. 3. 4.查看详细信息 5.记住这几个 6.选择属性开始配置 7. 8.只要把ip地址设置为同一网段下面的你想要的设置的空余ip即可,其他掩码和DNS不要修 ...

  3. 通过view获取所在的viewController对象

    建议写成UIView的分类,如下: .h - (UIViewController *)viewController; .m - (UIViewController *)viewController { ...

  4. element-ui的el-table的表头与列不对齐

    最好加到全局样式中: body .el-table th.gutter{ display: table-cell!important; }

  5. mysql 连接数据库时时区报错

    1.url: jdbc:mysql://192.168.0.101:3306/testdb?serverTimezone=UTC,在连接字符后面添加时区设置 2.使用navicat或者直接使用mysq ...

  6. Python实现简单中文词频统计示例

    简单统计一个小说中哪些个汉字出现的频率最高: import codecs import matplotlib.pyplot as plt from pylab import mpl mpl.rcPar ...

  7. Eth合约攻击

    前言 Ethernaut是一个类似于CTF的智能合约平台,集成了不少的智能合约相关的安全问题,这对于安全审计人员来说是一个很不错的学习平台,本篇文章将通过该平台来学习智能合约相关的各种安全问题,由于关 ...

  8. 9.2.3 hadoop reduce端连接-分区分组聚合

    1.1.1         reduce端连接-分区分组聚合 reduce端连接则是利用了reduce的分区功能将stationid相同的分到同一个分区,在利用reduce的分组聚合功能,将同一个st ...

  9. A way to use NAT network by using Oracle virtualBox

    That is true. Vmware  is easy and confortable tools to make vitrual machines than Oracle virtual box ...

  10. 使用gitthub 创建项目搭建博客

    最近在学Andrew Ng 的机器学习,做了上买那个的练习题和变成作业,想着把自己做的编程作业放到GitHub,方便讨论,虽然之前注册了GitHub,但这两年整天在毕设方向的事,做的身心俱疲,再加上自 ...