题目链接

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

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

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

这道题和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;
}

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

最新文章

  1. Linux 系统查看物理内存使用率的命令脚本,以百分比形式输出。
  2. 微信小程序的两个BUG?
  3. iOSQuartz2D-02-绘制炫酷的下载进度条
  4. java.注释类型
  5. linux 后台运行程序
  6. js中(function(){…})()立即执行函数写法理解(转载oschina)
  7. android studio 突然出现Gradle project sync failed 错误
  8. 如何修改UIButton按下后默认的蓝色效果
  9. VMWare Workstation:局域网PC连接虚拟机里的远程桌面或端口
  10. PHP 绘图技术
  11. Animate.css 一款牛逼的css3动画库
  12. 0_Simple__cppIntegration
  13. 实验MyOD
  14. Java中Math类的常用方法
  15. python书籍推荐:Head First Python(中文版)
  16. springboot学习四:整合mybatis
  17. GCC编译器原理(二)------编译原理一:ELF文件(2)
  18. jquery利用正则表达式验证密码,手机号(主要是使用方法,正则表达式网上一搜一堆)
  19. Django安装遇到的问题
  20. 修改linux服务器的MySQL密码

热门文章

  1. Java基础小知识(一)
  2. ssh: connect to git@gitlab.xxxxx.com:xxxxx.git port 22: Connection refused
  3. 配置Mongodb
  4. 你是否听说过 HashMap 在多线程环境下操作可能会导致程序死循环?
  5. SpringCloud Netflix Feign
  6. python面试的100题(11)
  7. StringBuilder与String的区别
  8. eclipse debug启动时tomcat报错
  9. 【转载】Java泛型(二)
  10. ACM-ICPC实验室20.2.22测试-动态规划