[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 ...
随机推荐
- Django模板(请用Django2.0版本完成)
1. 在learn目录下新建一个templates文件夹,里面新建一个home.html (1) 很简单的,就直接右键learn,新建文件夹,完成后,继续右键templates,创建文档,后缀名为ht ...
- WPF学习 - 自定义Panel
WPF中的Panel(面板),是继承自FrameworkElement的抽象类,表示一个可以用来排列子元素的面板. 在WPF中,一种预设了几种常用的面板,如Grid.StackPanel.WrapPa ...
- Pycharm包推荐|自动检查shell脚本问题的包
如图,这个包自动会检测出哪块代码编写有问题,自动提示,这里可以根据提示进行修改,快速高效!!! 包的名字如图:Shell script formatter 太香了
- HTML一键打包APK工具 如何进行实名认证购买和激活
HTML一键打包APK工具 价格表 授权时长 价格 1小时 49 1天 99 1个月 199 1个季度 399 半年 599 1年 799 付费版功能 功能点 免费版 付费版 去除广告信息 × √ 去 ...
- Seata AT和XA模式
一.分布式事务产生得原因: 1.1.数据库分库分表 当数据库单表一年产生的数据超过1000W,那么就要考虑分库分表,具体分库分表的原理在此不做解释,以后有空详细说,简单的说就是原来的一个数据库变成了多 ...
- 为什么大家都在用 WebP?
WebP 是谷歌在 2010 年提出的一种新型的图片格式,放到现在来讲,已经不算是"新"技术了,毕竟已经有了更新的 JPEG XL 和 AVIF .但是在日常工作中,大家时常会碰到 ...
- TOP GP 把已经编译的per反编回对应版本的4fd(画面档)
由于GP5.1,5.2,5.3的genero对应版本画面档开发互不兼容,下面提供各版本之间互转的操作方法: xshell切换到要反编译的per档目录,执行以下命令,就会在同目录下生成对应4fd档资料 ...
- Go 复合类型之切片类型介绍
Go 复合类型之切片类型 目录 Go 复合类型之切片类型 一.引入 二.切片(Slice)概述 2.1 基本介绍 2.2 特点 2.3 切片与数组的区别 三. 切片声明与初始化 3.1 方式一:使用切 ...
- 【最佳实践】MongoDB导入数据时重建索引
MongoDB一个广为诟病的问题是,大量数据resotore时索引重建非常缓慢,实测5000万的集合如果有3个以上的索引需要恢复,几乎没法成功,而且resotore时如果选择创建索引也会存在索引不生效 ...
- CSS 还原拉斯维加斯球数字动画
我的小册 <CSS 技术揭秘与实战通关>上线了,想了解更多有趣.进阶.系统化的 CSS 内容,可以猛击 - LINK. 最近大家刷抖音,是否有刷到拉斯维加斯的新地标 「Sphere」: 场 ...