题目链接

题目大意

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

解题思路

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

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

所以用\(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. Dapper简介

    Dapper文档 一,介绍:Dapper是一款轻量级ORM工具.如果你在小的项目中,使用Entity Framework.NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀.你又觉得 ...

  2. eclipse运用经验

    1.eclipse粘贴字符串添加转义符 2.eclipse的jdk版本切换 1.Window—Preferences—Java—Compiler—右侧面板设置为1.6 2.Window—Prefere ...

  3. MSSQL 打开xp_cmdshell

    sp_configure reconfigure go sp_configure reconfigure go

  4. Auto.js的初次使用——在VSCode中使用

    最近双十一大家都在集猫币,盖楼,但是每天刷任务太浪费时间了.被推荐了一个脚本可以自动刷任务,很是好奇.于是想要了解一下Auto.js 一.vscode启动Auto.js 1.vscode里安装auto ...

  5. 【Unity|C#】基础篇(0)——C#与.NET框架

    [学习资料] <C#图解教程>(第1章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...

  6. 2.sleep和wait的区别:

    sleep是Thread类的方法,wait是object(Java类库的老祖宗)的方法 sleep阻塞的线程在指定时间后,会转变为可执行状态:wait它要等待notify的唤醒 执行了sleep的线程 ...

  7. mysql 查询时间戳格式化 和thinkphp查询时间戳转换

    我在网上看了好多写的,都差不多,甚至好多都是一个人写的被别人转载啥的,哎 我写一个比较简单的 1.mysql语句 格式化时间戳 select id,name,FROM_UNIXTIME(time,'% ...

  8. 故障解决 | win10没声音及找不到Realtek高清音频管理器

    重装 win10 系统后,电脑没声音,更新驱动以及万不得已下载驱动精灵都没有解决. 后来发现在“硬件和声音”中没有Realtek高清音频管理器,之后找到解决办法如下: 1. 找到Realtek高清音频 ...

  9. HTML学习(4)属性

    属性是HTML元素提供的附加信息,大多数标签都能设置属性,一般位于开始标签,以名称/值的方式出现,例:name="value". 值要放在引号内(单引号.双引号都可以),如果值包含 ...

  10. ios 下 select和option 无法隐藏指定元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...