Codeforces 1313C.Skyscrapers
2024-10-08 12:05:32
题目链接
题意是给你一个数组,问你如何建造,使得每个点都不小于其左右的点,包括不相邻的点
分析题意,容易得知,就是找一个点两侧的不上升序列且带修,那我们就分别从头跑一遍,从尾跑一遍,两者相加就是每个点的最大值
那我们可以利用单调栈来进行这个操作,注意初始化栈
这道题和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;
}
求一个点两边的性质可以跑两遍来实现
最新文章
- Linux 系统查看物理内存使用率的命令脚本,以百分比形式输出。
- 微信小程序的两个BUG?
- iOSQuartz2D-02-绘制炫酷的下载进度条
- java.注释类型
- linux 后台运行程序
- js中(function(){…})()立即执行函数写法理解(转载oschina)
- android studio 突然出现Gradle project sync failed 错误
- 如何修改UIButton按下后默认的蓝色效果
- VMWare Workstation:局域网PC连接虚拟机里的远程桌面或端口
- PHP 绘图技术
- Animate.css 一款牛逼的css3动画库
- 0_Simple__cppIntegration
- 实验MyOD
- Java中Math类的常用方法
- python书籍推荐:Head First Python(中文版)
- springboot学习四:整合mybatis
- GCC编译器原理(二)------编译原理一:ELF文件(2)
- jquery利用正则表达式验证密码,手机号(主要是使用方法,正则表达式网上一搜一堆)
- Django安装遇到的问题
- 修改linux服务器的MySQL密码
热门文章
- Java基础小知识(一)
- ssh: connect to git@gitlab.xxxxx.com:xxxxx.git port 22: Connection refused
- 配置Mongodb
- 你是否听说过 HashMap 在多线程环境下操作可能会导致程序死循环?
- SpringCloud Netflix Feign
- python面试的100题(11)
- StringBuilder与String的区别
- eclipse debug启动时tomcat报错
- 【转载】Java泛型(二)
- ACM-ICPC实验室20.2.22测试-动态规划