传送门

题目大意

给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大。

题解

考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负,但是之后的所有数对答案的贡献都可以达到这些数的绝对值,即对于第一个左括号,钦定其对应的右括号在整个表达式的最后,这一段表达式内除去前缀的加法表达式外,对于所有的连续的加法外加一个括号,可以构造形如$$A-(x+x-(x+x+x)-x-x-(x+x))$$使得贡献全部为正但是题目中还有每个括号必须与一个数相邻,不过无伤大雅,因为形如$-(a-(b+c))$等价于$-(a-b)+c$。

因而引出两种解法。

解法一:$DP$

考虑上述构造方法一定满足括号层数不超过$2$,所以可以直接令$F_{i,j=0,1,2}$表示到第$i$个位置有$j$个左括号尚未匹配的答案,转移过程显然。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 100020
#define INF 100000000000000000ll
using namespace std;
int read(){
int nm=0,fh=1; char cw=getchar();
for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
return nm*fh;
}
LL n,x,F[3];
int main(){
n=read(),F[1]=F[2]=-INF;
while(n--){
x=read(),F[0]+=x,F[1]-=x,F[2]+=x;
if(x<0) F[2]=max(F[1],F[2]),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;
}

解法二:贪心

直接用上构造出来的性质,将形如$+a+b+c$的表达式缩成$+x$,保证不会出现两个连续的$+x$,接着枚举第一个左括号的位置,形如$G\space -\space (x+y\space +K$($y$可能不存在)$G$表示前半部分表达式的值,$K$表示后半部分的绝对值的和,用$G+K-|x|-|y|$的值之和更新答案,不要忘了用特判所有符号均为正的情况。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 100020
using namespace std;
LL read(){
LL nm=0,fh=1; LL cw=getchar();
for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
return nm*fh;
}
LL n,p[M],G[M],F[M],ans;
int main(){
n=read();
for(LL i=1;i<=n;i++){
p[i]=read();
if(i>1&&p[i]>0&&p[i-1]>0) p[i-1]+=p[i],i--,n--;
}
for(LL i=1;i<=n;i++) G[i]=G[i-1]+p[i],F[i]=F[i-1]+abs(p[i]); ans=G[n];
for(LL i=2;i<=n;i++) if(p[i-1]<0) ans=max(ans,G[i-1]-p[i]+F[n]-F[i]);
printf("%lld\n",ans); return 0;
}

Arc066_E 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. E - Addition and Subtraction Hard AtCoder - 2273 思维观察题

    http://arc066.contest.atcoder.jp/tasks/arc066_c?lang=en 这类题目是我最怕的,没有什么算法,但是却很难想, 这题的题解是这样的,观察到,在+号里面 ...

随机推荐

  1. 【BZOJ2466】[中山市选2009]树 树形DP

    [BZOJ2466][中山市选2009]树 Description 图论中的树为一个无环的无向图.给定一棵树,每个节点有一盏指示灯和一个按钮.如果节点的按扭被按了,那么该节点的灯会从熄灭变为点亮(当按 ...

  2. linux字符集查看与设置

    linux字符集查看与设置 命令:locale -a   查看本地的字符集        locale -m 查看所有支持的字符集   查看当前默认设置   echo $LANG   记录系统默认使用 ...

  3. Js中的Object.defineProperty

    通过Object.defineProperty为对象设置属性,并同时规定属性的属性(可见性,可配置性,可枚举性等) 备注:如果通过var obj = {} obj.age = 18这种方式设置的属性, ...

  4. easyui的 一些经验

    1. 渲染网络表格时,行操作 <th field="sort_num" width="10" data-options="field:'id', ...

  5. 什么是gevent

    gevent是一个基于协程的python网络库,它使用greenlet在libev或libuv事件循环之上提供高级同步API 功能包括 基于libev或libuv的快速时间循环 基于greenlets ...

  6. 【leetcode刷题笔记】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. IAR 条件断点

    条件断点是IDE的一个重要功能,在IAR调试时候,经常跟踪一个数据,但是对较大的buffer,用普通的断点或live watch都不好跟踪. 比如某个buffer里一个数,我们知道他在第几个,但是却从 ...

  8. React Native 列表的总结

    React Native 列表的总结 FlatList和SectionList都是React Native中高性能的列表组件.这些新的列表组件在性能方面都有了极大的提升, 其中最主要的一个是无论列表有 ...

  9. UI控件概述

    常见UI控件 UIKit框架提供了非常多功能强大又易用的UI控件,以便于开发者打造出各式各样的App 以下列举一些在开发中常见的UI控件(稍后补上图片示例) 1.UILabel– 文本标签:作用是显示 ...

  10. Struts基本原理 + 实现简单登录(二)

    MVC 概念 MVC全名是Model View Controller,是模型(model)—视图(view)—控制器(controller)的缩写,知道这么多就够了. 大家都知道SUN公司对于MVC模 ...