Arc066_E Addition and Subtraction Hard
题目大意
给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大。
题解
考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负,但是之后的所有数对答案的贡献都可以达到这些数的绝对值,即对于第一个左括号,钦定其对应的右括号在整个表达式的最后,这一段表达式内除去前缀的加法表达式外,对于所有的连续的加法外加一个括号,可以构造形如$$A-(x+x-(x+x+x)-x-x-(x+x))$$使得贡献全部为正但是题目中还有每个括号必须与一个数相邻,不过无伤大雅,因为形如$-(a-(b+c))$等价于$-(a-b)+c$。
因而引出两种解法。
解法一:$DP$
考虑上述构造方法一定满足括号层数不超过$2$,所以可以直接令$F_{i,j=0,1,2}$表示到第$i$个位置有$j$个左括号尚未匹配的答案,转移过程显然。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 100020
#define INF 100000000000000000ll
using namespace std;
int read(){
int nm=0,fh=1; char cw=getchar();
for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
return nm*fh;
}
LL n,x,F[3];
int main(){
n=read(),F[1]=F[2]=-INF;
while(n--){
x=read(),F[0]+=x,F[1]-=x,F[2]+=x;
if(x<0) F[2]=max(F[1],F[2]),F[1]=max(F[1],F[0]);
F[0]=max(F[0],F[1]),F[1]=max(F[1],F[2]);
} printf("%lld\n",F[0]); return 0;
}
解法二:贪心
直接用上构造出来的性质,将形如$+a+b+c$的表达式缩成$+x$,保证不会出现两个连续的$+x$,接着枚举第一个左括号的位置,形如$G\space -\space (x+y\space +K$($y$可能不存在)$G$表示前半部分表达式的值,$K$表示后半部分的绝对值的和,用$G+K-|x|-|y|$的值之和更新答案,不要忘了用特判所有符号均为正的情况。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 100020
using namespace std;
LL read(){
LL nm=0,fh=1; LL cw=getchar();
for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
return nm*fh;
}
LL n,p[M],G[M],F[M],ans;
int main(){
n=read();
for(LL i=1;i<=n;i++){
p[i]=read();
if(i>1&&p[i]>0&&p[i-1]>0) p[i-1]+=p[i],i--,n--;
}
for(LL i=1;i<=n;i++) G[i]=G[i-1]+p[i],F[i]=F[i-1]+abs(p[i]); ans=G[n];
for(LL i=2;i<=n;i++) if(p[i-1]<0) ans=max(ans,G[i-1]-p[i]+F[n]-F[i]);
printf("%lld\n",ans); return 0;
}
Arc066_E Addition and Subtraction Hard的更多相关文章
- [leetcode-592-Fraction Addition and Subtraction]
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- LC 592. Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- 大数据加减(Big data addition and subtraction)
题目描述 Description 加减法是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(不超过1000位)的加减法算式,请给出正确结果.为提高速度,保证给定 ...
- E - Addition and Subtraction Hard AtCoder - 2273 思维观察题
http://arc066.contest.atcoder.jp/tasks/arc066_c?lang=en 这类题目是我最怕的,没有什么算法,但是却很难想, 这题的题解是这样的,观察到,在+号里面 ...
随机推荐
- [Java开发之路](8)输入流和输出流
1. Java流的分类 按流向分: 输入流: 能够从当中读入一个字节序列的对象称作输入流. 输出流: 能够向当中写入一个字节序列的对象称作输出流. 这些字节序列的来源地和目的地能够是文件,并且通常都是 ...
- Hadoop生态优秀文章集锦
如何用形象的比喻描述大数据的技术生态?Hadoop.Hive.Spark 之间是什么关系? https://www.zhihu.com/question/27974418 HBase 和 Hive 的 ...
- 解决Eclipse的Team菜单中没有SVN选项的问题
我们想使用SVN向SVN服务器上传代码,但Eclipse默认情况下却没有SVN选项,如下图所示. 默认只有GIT,如下图所示. 那么,我们怎么解决这个问题呢? 第一步:如下图所示. 第二步:在&quo ...
- Python高级入门01-property
JAVA中存在对变量 私有化,公开,保护... 私有化时候,需要提供一个公开的get 和 set方法对外公开,让别人进行调用 python中同样存在 私有化变量定义是__是这个双下划线,eg:_ ...
- mysql数据库访问授权
1.进入MySQL服务器 d:\mysql\bin\> mysql -h localhost -u root; 2.赋予任何主机访问数据的权限 mysql> GRANT ALL PRIVI ...
- Opennms -安装
参考官方网站:https://docs.opennms.org/opennms/releases/latest/guide-install/guide-install.html#gi-install- ...
- 蜗牛—ORACLE基础之触发器学习(三)
版权声明:本文为大腰子原创文章,如若转载,请标明原地址. https://blog.csdn.net/u010071361/article/details/30037215 建立一个触发器, 当职工表 ...
- urllib2下载网页的三种方法
1.最直接的方法 #-*- coding: utf-8 -*- import urllib2 #直接请求 response = urllib2.urlopen('https://www.baidu.c ...
- 33_为应用添加多个Activity与参数传递
1\ 2\ 3\ 4\ 2 3
- Block的详细介绍
关于block的介绍 ==ios中的内存空间分级== 栈区 存放函数参数值.局部变量.函数返回地址等,函数跳转跳转时现场保护(寄存器),这些系统都会帮我们自动实现,无需我们干预. 所以大量的局部变量, ...