题目链接

题目大意

给定一个只含加减和数字的表达式,在其中添加括号,使其值最大。

解题思路

显然,只有减号后面的括号会使其中表达式的值取反。

然后只有已经有左括号时才能加入右括号。

所以用\(f_0\)表示没有左括号,用\(f_1\)表示当前是负区间,\(f_1\)表示当前是正区间。

当当前的数是负的时,可以加入左括号转移。当存在左括号时,可以加入右括号转移。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#define N 100010
#define ll long long
using namespace std;
ll f[3];
void read(ll &x)
{
x=0;
int ch=0,flag=1;
for(;!isdigit(ch);ch=getchar())
if(ch=='-') flag=-flag;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
x*=flag;
}
int main()
{
int n;
scanf("%d",&n);
f[0]=0,f[1]=f[2]=-10000000000000ll;
for(int i=1;i<=n;i++)
{
ll x;
read(x);
f[0]+=x;
f[1]-=x;
f[2]+=x;
if(x<0)
{
f[2]=max(f[2],f[1]);
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;
}

arc066E - Addition and Subtraction Hard的更多相关文章

  1. [leetcode-592-Fraction Addition and Subtraction]

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  2. [LeetCode] Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  3. [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  4. 592. Fraction Addition and Subtraction

    Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...

  5. [LeetCode] 592. Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  6. LC 592. Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  7. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

  8. 大数据加减(Big data addition and subtraction)

    题目描述 Description 加减法是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(不超过1000位)的加减法算式,请给出正确结果.为提高速度,保证给定 ...

  9. Arc066_E Addition and Subtraction Hard

    传送门 题目大意 给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大. 题解 考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负, ...

随机推荐

  1. P4562 [JXOJ2018]游戏

    题目描述 她长大以后创业了,开了一个公司. 但是管理公司是一个很累人的活,员工们经常背着可怜偷懒,可怜需要时不时对办公室进行检查. 可怜公司有 n 个办公室,办公室编号是 l 到 l+n−1 ,可怜会 ...

  2. Mysql使用事务

    DECLARE t_error INTEGER DEFAULT 0; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; START TR ...

  3. input如何上传文件

    1)绑定input[type='file']的change事件 <input @change="uploadPhoto($event)" type="file&qu ...

  4. 密码学笔记——zip明文攻击

    明文攻击(Known plaintext attack):是一种攻击模式,指攻击者已知明文.密文及算法,求密钥的过程. 例题: 这就是一个坑 密码是十位大小写字母.数字.特殊符号组成的,你爆破的开么? ...

  5. AcWing 790. 数的三次方根

    #include<bits/stdc++.h> using namespace std ; int main(){ double x; cin>>x; ,r=; ) { ; i ...

  6. find & grep 总 结

    前言 关于本文 总 结 了 find.grep常 规 用 法,正 则 表 达 式,find与 grep合 用 以 及 自 定 义 搜 索 函 数 等 什么是find和grep find 和 grep ...

  7. Tomcat无法成功启动——双击startup.bat闪退

    使用的Tomcat是免安装版本的.因为在启动tomcat是需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了tomcat的闪退. 解决办法: 1:在已解压的tomcat的bin文件 ...

  8. [lua]紫猫lua教程-命令宝典-L1-01-11. lua的个人补充

    1.关于三目运算符的一些补充和纠正 前面没看仔细  a>b ? a: b 这个形式 似乎lua下并不存在...要了命 一般都是使用  a and b or c 的形式 但是这种形式存在一些问题 ...

  9. python调用sqlite

    参考资料:https://www.liaoxuefeng.com/wiki/1016959663602400/1017801751919456  https://www.cnblogs.com/lia ...

  10. 【转载】Java多线程

    转自:http://www.jianshu.com/p/40d4c7aebd66 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目 ...