题目链接

题意是给你一个数组,问你如何建造,使得每个点都不小于其左右的点,包括不相邻的点

分析题意,容易得知,就是找一个点两侧的不上升序列且带修,那我们就分别从头跑一遍,从尾跑一遍,两者相加就是每个点的最大值

那我们可以利用单调栈来进行这个操作,注意初始化栈

这道题和CF1300E思想一样,都是用单调栈求最大不下降序列的和值,且可以进行修改

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii; const int maxn = 500003;
int buf[maxn], q[maxn];
LL s[maxn]; void run_case() {
int n; cin >> n;
for(int i = 1; i <= n; ++i) cin >> buf[i];
LL sum = 0;
int top = 0;
q[0] = 0; // init stack
for(int i = 1; i <= n; ++i) {
while(top && buf[i] < buf[q[top]]) {
int now = q[top--];
sum -= 1LL * (now - q[top]) * buf[now];
}
sum += 1LL * (i - q[top]) * buf[i];
s[i] += sum;
q[++top] = i;
}
sum = 0, top = 0;
q[0] = n+1; // init stack
for(int i = n; i >= 1; --i) {
while(top && buf[i] < buf[q[top]]) {
int now = q[top--];
sum -= 1LL * (q[top] - now) * buf[now];
}
sum += 1LL * (q[top] - i) * buf[i];
s[i] += sum - buf[i];
q[++top] = i;
}
int pos = max_element(s+1, s+1+n) - s;
for(int i = pos-1; i >= 1; --i)
buf[i] = min(buf[i], buf[i+1]);
for(int i = pos+1; i <= n; ++i)
buf[i] = min(buf[i], buf[i-1]);
for(int i = 1; i <= n; ++i)
cout << buf[i] << " ";
} int main() {
ios::sync_with_stdio(false), cin.tie(0);
cout.flags(ios::fixed);cout.precision(10);
//int t; cin >> t;
run_case();
cout.flush();
return 0;
}

求一个点两边的性质可以跑两遍来实现

Codeforces 1313C.Skyscrapers的更多相关文章

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

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

  2. 【Codeforces 1137A】Skyscrapers

    Codeforces 1137 A 题意:给一个矩阵,问对于每一个格子\((x,y)\),把第\(x\)行和第\(y\)列的值取出,要求将它们每一个赋上一个正整数,要求同一行.列中大小关系依然相同,问 ...

  3. 【codeforces 335E】 Counting Skyscrapers

    http://codeforces.com/problemset/problem/335/E (题目链接) 题意 懒得写了= = Solution 这题咋不上天= =. 参考题解:http://blo ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Zenject与UniRx结合实现跨线程通信Signal

    修改Zenject下ProfileBlock.cs源码, 取消有关UnityEngine.Profiling.Profiler的代码. 然后使用Zenject的Signal: // 定义Signal ...

  2. nginx.conf 配置记录

    #user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log lognginxs/error.log; ...

  3. Python与C语言基础对比(Python快速入门)

    代码较长,建议使用电脑阅读本文. 10分钟入门Python 本文中使用的是Python3 如果你曾经学过C语言,阅读此文,相信你能迅速发现这两种语言的异同,达到快速入门的目的.下面将开始介绍它们的异同 ...

  4. QImage和IplImage转换总结

    在arm中做图像处理,因为不支持GTK,一般都会用到QT来实现显示功能,所以不可避免的要涉及到QImage和IplImage两种图像格式之间的转换,下面总结一下转换的方法. (下面格式转换的代码都是网 ...

  5. 只想remove parentNode的一部分children

    parentNode.removeChildByTag(0); let childNode = new cc.Node(); parentNode.addChild(childNode); child ...

  6. 「题解」「CF1103B」Game with modulo

    简易中文题目 猜一个数字 \(a\),而你可以向机器提问一对 \((x,y)\) ,如果 \(x\bmod a\ge y \bmod a\) 机器返回字符串 x,反之返回字符串 y . 询问不能超过 ...

  7. 工具使用:xmind

    概念 心智图,又称脑图.思维导图.灵感触发图.概念地图或思维地图,是一种图像式思维的工具与及一种利用图像式思考辅助工具来表达思维的工具. 详细的可以查看这里(维基百科)还有这里(百度百科) 用了思维导 ...

  8. 题解【洛谷P4588】[TJOI2018]数学计算

    题目描述 小豆现在有一个数\(x\),初始值为\(1\).小豆有\(Q\)次操作,操作有两种类型: \(1\;m\):\(x=x\times m\)输出\(x\%mod\); \(2\;pos\):\ ...

  9. WOW Factor

    Recall that string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of s ...

  10. 【转载】Java的JDBC事务详解

    转自:http://www.cnblogs.com/azhqiang/p/4044127.html 事务的特性: 1) 原子性(atomicity):事务是数据库的逻辑工作单位,而且是必须是原子工作单 ...