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
传送门 题目大意 给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大. 题解 考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负, ...
随机推荐
- Vue中父组件向子组件echarts传值问题
原文链接:https://blog.csdn.net/Uookic/article/details/80638883?utm_source=copy 问题:当父组件传值给子组件echarts时,发现子 ...
- 「题解」「CF850A」Five Dimensional Points
题目 点这里 题解 本题暴力可过,细节不必多说. 这里我主要是说明一下为什么当 \(n>11\) 时可以直接输出 \(0\) . 首先,思考二维空间中,我们能保证最多能同时存在多少点,而还有好点 ...
- Iris路由和路由组
package main import ( "github.com/kataras/iris" "github.com/kataras/iris/context" ...
- c++ google glog模块安装和基本使用(ubuntu)环境
1,如何安装 1 Git clone https://github.com/google/glog.git 2 cd glog 3 ./autogen.sh 4 ./configure --prefi ...
- 使用git上传项目解决码云文件次数上传限制(原文)
起因:个人免费版的码云上传文件时限制: 1个小时内只能上传20个文件 解决方法:在码云创建空的项目仓库,使用git客户端下载码云的项目,把需要上传的文件复制到该项目中去,用git提交! 1.配置git ...
- Axure licensee key 8~9-转
转:https://7rp.cn/34 AxureRP v9.0.0.3646 正式版 — 亲测可用 Licensee: jasmine Key: ATocOwMG75ijKpF0OEDSHQ3UZQ ...
- Docker - Docker Engine 结构结构概述
概述 Docker Engine 结构的简单描述 ref docker 实战 第一本 docker 书 1. docker 版本 1. 版本 Docker Engine - Community 概述 ...
- 【PAT甲级】1105 Spiral Matrix (25分)
题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...
- CF1272C
Recently, Norge found a string s=s1s2…sns=s1s2…sn consisting of nn lowercase Latin letters. As an ex ...
- ALSA driver --PCM 实例创建过程
前面已经写过PCM 实例的创建框架,我们现在来看看PCM 实例是如何创建的. 在调用snd_pcm_new时就会创建一个snd_pcm类型的PCM 实例. struct snd_pcm { struc ...