[ABC263D] Left Right Operation
Problem Statement
You are given an integer sequence of length $N$: $A=(A_1,A_2,\ldots,A_N)$.
You will perform the following consecutive operations just once:
Choose an integer $x$ $(0\leq x \leq N)$. If $x$ is $0$, do nothing. If $x$ is $1$ or greater, replace each of $A_1,A_2,\ldots,A_x$ with $L$.
Choose an integer $y$ $(0\leq y \leq N)$. If $y$ is $0$, do nothing. If $y$ is $1$ or greater, replace each of $A_{N},A_{N-1},\ldots,A_{N-y+1}$ with $R$.
Print the minimum possible sum of the elements of $A$ after the operations.
Constraints
- $1 \leq N \leq 2\times 10^5$
- $-10^9 \leq L, R\leq 10^9$
- $-10^9 \leq A_i\leq 10^9$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $L$ $R$
$A_1$ $A_2$ $\ldots$ $A_N$
Output
Print the answer.
Sample Input 1
5 4 3
5 5 0 6 3
Sample Output 1
14
If you choose $x=2$ and $y=2$, you will get $A = (4,4,0,3,3)$, for the sum of $14$, which is the minimum sum achievable.
Sample Input 2
4 10 10
1 2 3 4
Sample Output 2
10
If you choose $x=0$ and $y=0$, you will get $A = (1,2,3,4)$, for the sum of $10$, which is the minimum sum achievable.
Sample Input 3
10 -5 -3
9 -6 10 -1 2 10 -1 7 -15 5
Sample Output 3
-58
$L$, $R$, and $A_i$ may be negative.
设把区间 \([1,l-1]\) 全部变成 \(L\),把区间 \([r+1,n]\) 全部变成 \(R\),其余不变。预处理出数列 \(a\) 的前缀和 \(s\).
此时的总价值为 $$(s_r-s_{l-1})+L(l-1)+R(n-r)$$
\]
枚举 \(r\),要找到最大的 \(s_{l-1}-lL\),在枚举的同时维护最小值就可以了。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,l,r,a[N];
long long s[N],t[N],ans=1e18;
int main()
{
scanf("%d%d%d",&n,&l,&r);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
s[i]=s[i-1]+a[i];
ans=min(ans,1LL*i*l+1LL*(n-i)*r);
}
ans=min(ans,s[n]);
for(int i=0;i<=n;i++)
t[i]=max(t[i-1],s[i]-1LL*i*l);
for(int i=0;i<=n;i++)
ans=min(ans,s[i]-t[i-1]+1LL*n*r-1LL*i*r);
printf("%lld",ans);
}
[ABC263D] Left Right Operation的更多相关文章
- 终端mysql Operation not permitted错误解决方案
前言 前段时间装mysql,就遇到了ln: /usr/bin/mysql: Operation not permitted的错误,网上好多方法都过时了,下边是我的解决方法 原因 这是因为苹果在OS X ...
- SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...
- Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b error: Illegal ...
- TNS-12535: TNS:operation timed out案例解析
一数据库突然连接不上,在自己电脑上使用SQL Developer也连接不上.立即使用SecureCRT连接上了这台服务器,从下面几个方面检查. 1:检查了数据库的状态是否正常 $ sqlplus / ...
- 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted
1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...
- the user operation is waiting
eclipse在编辑完代码保存的时候,弹出一个进度框,等N长时间,标题是"user operation is waiting",里面显示的是building workspace的进 ...
- Python安装pywinauto时遇到error: The read operation timed out解决方法
Python结合Pywinauto 进行 Windows UI 自动化,安装pywinauto时遇到的一些问题: 解决方法:很明显是链接超时国外网站你懂的V_P_N吧,直接通过报错信息的链接复制到浏览 ...
- svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法
今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...
- windows下安装kibana出 "EPERM: operation not permitted
D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
随机推荐
- 深度学习(十二)——神经网络:搭建小实战和Sequential的使用
一.torch.nn.Sequential代码栗子 官方文档:Sequential - PyTorch 2.0 documentation # Using Sequential to create a ...
- 14.4K Star,一款外观漂亮、运行快速、动画细腻的开源免费UI组件库
之前给大家推荐了很多后台模版,有读者希望推荐一些跟通用的好看组件,毕竟出了后台还有很多其他场景嘛.所以,今天继续给大家推荐一个广受好评的UI组件库:NextUI 主要特性 NextUI的主要目标是简化 ...
- 使用 Rancher 安装 K8s 集群
舞台环境 Ubuntu 22.04.2 LTS Docker 24.0.2 2GB RAM或者更多 CPU 2核心或者更多 Rancher 2.6.9 测试环境中,我准备了两台 Ubuntu 服务器, ...
- 《最新出炉》系列入门篇-Python+Playwright自动化测试-15-playwright处理浏览器多窗口切换
1.简介 浏览器多窗口的切换问题相比大家不会陌生吧,之前宏哥在java+selenium系列文章中就有介绍过.大致步骤就是:使用selenium进行浏览器的多个窗口切换测试,如果我们打开了多个网页,进 ...
- webgl 刷底色的基本步骤
1.在html中建立画布 <canvas id="canvas"><canvas> 2.在js中获取canvas画布 const canvas = docu ...
- Vue源码学习(六):(支线)渲染函数中with(),call()的使用以及一些思考
好家伙, 昨天,在学习vue源码的过程中,看到了这个玩意 嘶,看不太懂,研究一下 1.上下文 这段出现vue模板编译的虚拟node部分 export function renderMixin( ...
- 基于FPGA 的SDRAM控制器
SDRAM基本信息 储存能力计算 4X16X4=256(Mbit),注意不是MByte SDRAM控制 sdram包含两个部分:sdram_ctrl.fifo_ctrl. sdram_ctrl:其顶层 ...
- ddddocr1.4.8失效的解决方法
1. 问题描述 from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.max ...
- 【matplotlib 实战】--散点图
散点图,又名点图.散布图.X-Y图,是将所有的数据以点的形式展现在平面直角坐标系上的统计图表. 散点图常被用于分析变量之间的相关性.如果两个变量的散点看上去都在一条直线附近波动,则称变量之间是线性相关 ...
- NewStarCTF 2023 公开赛道 WEEK1|CRYPTO全解
一.brainfuck 附件信息 ++++++++[>>++>++++>++++++>++++++++>++++++++++>++++++++++++> ...