arc066E - Addition and Subtraction Hard
题目链接
题目大意
给定一个只含加减和数字的表达式,在其中添加括号,使其值最大。
解题思路
显然,只有减号后面的括号会使其中表达式的值取反。
然后只有已经有左括号时才能加入右括号。
所以用\(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的更多相关文章
- [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位)的加减法算式,请给出正确结果.为提高速度,保证给定 ...
- Arc066_E Addition and Subtraction Hard
传送门 题目大意 给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大. 题解 考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负, ...
随机推荐
- CentOS7.5升级OpenSSH
实验环境 OS:CentOS 7.5 当前openssh版本:OpenSSH_7.4p1 升级后的openssh版本:OpenSSH_8.0p1 开通telnet 为了防止升级过程中ssh断连,保险起 ...
- leetcode 869. Reordered Power of 2
function reorderedPowerOf2(N) { var a1 = N.toString().split('') a1.sort((a, b) => a.localeCompare ...
- php核心技术与最佳实践 --- 错误与异常
<?php /*php error*/ /* * 异常和错误的概念不一样 * 在PHP里,遇到任何自身错误都会触发一个错误,而不是抛出异常(对于一些情况,会同时抛出异常和错误) * 异常处理机制 ...
- Linux线程间同步的几种方式
信号量 信号量强调的是线程(或进程)间的同步:"信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候,就阻塞 ...
- Harmonic Number (II) 数学找规律
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int ...
- c# DPI SCale
public class Screen { /// Primary Screen #region Win32 API [DllImport("user32.dll")] stati ...
- java包装类型的一些知识点
关键字:包装类的缓存,包装类之间数值的比较 来源:https://www.cnblogs.com/hdwang/p/7009449.html https://www.cnblogs.com/Dream ...
- mybatis(六):设计模式 - 组合模式
- SpringBoot集成mybatis以及自动化测试代码实现
Mybatis和logback的应用配置 1.在module的pom.xml文件中,加载springboot和swagger.lombok.fastjson.mysql.mybatis包 2.在res ...
- 【学习笔记】《Java编程思想》 第8~11章
第八章 多态 多态的条件: 1. 要有继承 2.父类对象引用子类对象 3. 要有方法的重写 多态的作用:消除类型之间的耦合关系. 将一个方法调用与一个方法主体关联起来称作绑定.若在程序执行前进行绑定, ...