题目链接

题目大意

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

解题思路

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

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

所以用\(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. JavaScript对象之属性操作

    在js对象中,我们可以对对象属性进行操作. 上图的要点为:for-in会把原型链上的可枚举属性也列出来. 上图的要点为:可以使用逻辑运算符&&进行层层查找对象是否为undefined, ...

  2. springmvc的框架搭建及工作流程

    1.搭建要点 web.xml: <servlet-mapping>     <servlet-name>springDispatcherServlet</servlet- ...

  3. 1-Django2.2安装指南

    django快速安装指南 作为一个Python Web框架,Django需要Python环境.下面是Django需要对应的python版本. Django版本 python版本 1.11 2.7, 3 ...

  4. python3爬取高清壁纸(2)

    上次只是爬取一个专辑的图片,这次要爬取一整个页面的所有专辑的图片. 在上次的代码的基础上进行修改就行了,从专辑的索引页面开始,爬取该页面上所有的专辑的链接,再套用上次的代码就行了. 若要爬取多个页面只 ...

  5. bbs论坛注册功能(1)

    分析项目需求创建表: STATICFILE_DIR = [ os.path.join(BASE_DIR,'static') #设置目录,bootstrip添加到目录中去,直接本地调用 ] # auth ...

  6. 题解【2.23考试T2】str

    2. str [题目描述] 这是一道传统题,源代码的文件名为 str.cpp/c/pas. 构造 n 个 01 字符串 S1...Sn,使得对于任意 i≠j,Si 不是 Sj 的前缀.在最小化串长和的 ...

  7. 调用百度地图api隐藏版权信息

    调用百度地图API隐藏右下角版权信息 商用的话建议不要隐藏,避免侵权. 隐藏前: 隐藏后: .BMap_cpyCtrl { display: none; } .anchorBL { display: ...

  8. LED Decorative Light Manufacturer Introduction: LED Metal Table Light

    Nowadays, when many people choose the desk light, they are worried that it will not be used for a lo ...

  9. Python spyder-快捷键-多行注释

    选中多行后: Ctrl + 1: 注释/反注释 Ctrl + 4/5: 块注释/块反注释 Ctrl + L: 跳转到行号 Tab/Shift + Tab: 代码缩进/反缩进

  10. DataGridView单元格显示密码

    DataGridView单元格显示密码 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormatt ...