题目链接

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

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

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

这道题和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. echarts 如何去掉折线上的小圆点

    series:[{ symbol:none; //去掉折线上的小圆点 type:line; name:seriesName; data:seriesData }]

  2. 这里有一份热乎乎的git相关操作

    文件操作 git init (添加文件): git status (查看文件状态): git diff (查看修改内容): git rm (删除文件): git add (把文件保存在暂存区): gi ...

  3. Apache Kafka(八)- Kafka Delivery Semantics for Consumers

    Kafka Delivery Semantics 在Kafka Consumer中,有3种delivery semantics,分别为:至多一次(at most once).至少一次(at least ...

  4. java基础(七)之子类实例化

    知识点;1.生成子类的过程2.使用super调用父类构造函数的方法 首先编写3个文件. Person.java class Person{ String name; int age; Person() ...

  5. 动态设置微信小程序 navigationBarTitle 的值

    wx.setNavigationBarTitle({ title:' 动态值 ' })

  6. springboot项目入门解析

                     config:主要用来存储配置文件,以及其他不怎么动用的信息. controller:项目的主要控制文件 dao:           主要用来操作数据库 entit ...

  7. jQuery 源码解析(三十) 动画模块 $.animate()详解

    jQuery的动画模块提供了包括隐藏显示动画.渐显渐隐动画.滑入划出动画,同时还支持构造复杂自定义动画,动画模块用到了之前讲解过的很多其它很多模块,例如队列.事件等等, $.animate()的用法如 ...

  8. 计算几何-Dot-Vector

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 看了书,然后码 ...

  9. 「模板」AC自动机

    目录 说明 普通版本 询问更改版 拓扑优化版本 说明 这篇博客只挂模板,具体分析请膜拜大佬 hyfhaha 大佬. 普通版本 题目传送门 #include<cstdio> #include ...

  10. opencv:轮廓逼近与拟合

    轮廓逼近,本质上是减少编码点 拟合圆,生成最相似的圆或椭圆 #include <opencv2/opencv.hpp> #include <iostream> using na ...