CF1313C2 Skyscrapers (hard version)
思路:
使用单调栈。
实现:
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)的更多相关文章
- Codeforces Round #622(Div 2)C2. Skyscrapers (hard version)
题目链接 : C2. Skyscrapers (hard version) 题目描述 : 与上一道题类似,只是数据范围变大, 5e5, 如果用我们原来的方法,铁定是超时的. 考察点 : 单调栈,贪心, ...
- Codeforces Round #622(Div 2) C1. Skyscrapers (easy version)
题目链接: C1. Skyscrapers (easy version) 题目描述: 有一行数,使得整个序列满足 先递增在递减(或者只递增,或者只递减) ,每个位置上的数可以改变,但是最大不能超过原来 ...
- Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)
Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 题意: 你是一名建筑工程师,现给出 n 幢建筑的预计建设高度,你想建成峰状, ...
- 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 ...
- 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 ...
- Codeforces Round #622 (Div. 2) C2 - Skyscrapers (hard version) 单调栈
从左往右扫,找到比第i个小的第一个数字,l[i] = l[last] + (i - last) * m[i],用单调栈O(n)维护这个过程,再从右往左扫,同理可以算出r数组,注意一下long long ...
- Codeforces Round #622 (Div. 2).C2 - Skyscrapers (hard version)
第二次写题解,请多多指教! http://codeforces.com/contest/1313/problem/C2 题目链接 不同于简单版本的暴力法,这个数据范围扩充到了五十万.所以考虑用单调栈的 ...
- C2 - Skyscrapers (hard version)
前几天做的题,当时好像是超时了,这个博客写的超好https://blog.csdn.net/lucky52529/article/details/89155694 用单调站解决问题. 代码是从另外一篇 ...
- X - Skyscrapers (hard version) CodeForces - 1313C2
题目大意:n个高楼,每个楼最高为mi,要求,第i个楼左边和右边不能有同时比它高的楼.让你求最在n个楼总和最高的情况下,每个楼的高度. 题解:用单调栈来做,n个楼的高度要么是单调递减,要么是单调递增,要 ...
随机推荐
- LeetCode200 岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- 大数相加Java
题目 以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回. 分析 两个字符串,定义两个指针,分别从这两个字符串的结尾开始遍历,因为可能字符串1比字符串2长度要长,因此只要两者其中有 ...
- 实现strStr
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Spring框架之websocket源码完全解析
Spring框架之websocket源码完全解析 Spring框架从4.0版开始支持WebSocket,先简单介绍WebSocket协议(详细介绍参见"WebSocket协议中文版" ...
- Head First 设计模式 —— 15. 与设计模式相处
模式 是在某情境(context)下,针对某问题的某种解决方案. P579 情景:应用某个模式的情况 问题:你想在某情境下达到的目标,但也可以是某情境下的约束 解决方案:一个通用的设计,用来解决约束. ...
- 【Oracle】删除表空间
删除表空间如果是 SQL> DROP TABLEPSACE XXXX; 是无法将数据文件一同都删除的 想要删除表空间和数据文件需要如下操作: SQL> drop tablespace XX ...
- 【Oracle】查询锁的相关SQL
--查看有锁的进程 select t2.username,t2.sid,t2.serial#,t2.logon_time,t2.state from v$locked_object t1,v$sess ...
- 通过 profiling 定位 golang 性能问题 - 内存篇 原创 张威虎 滴滴技术 2019-08-02
通过 profiling 定位 golang 性能问题 - 内存篇 原创 张威虎 滴滴技术 2019-08-02
- python 使用函数名的字符串调用函数(4种方法)_black-heart的专栏-CSDN博客 https://blog.csdn.net/mrqingyu/article/details/84403924
funcs = ['fetch_data_' + i for i in ( 'activities', 'banners', 'server_list')]# from operator import ...
- Java Socket实战之七 使用Socket通信传输文件
http://blog.csdn.net/kongxx/article/details/7319410 package com.googlecode.garbagecan.test.socket.ni ...