传送门

题目大意

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

题解

考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负,但是之后的所有数对答案的贡献都可以达到这些数的绝对值,即对于第一个左括号,钦定其对应的右括号在整个表达式的最后,这一段表达式内除去前缀的加法表达式外,对于所有的连续的加法外加一个括号,可以构造形如$$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. 一篇文章彻底弄清ARC始末

    本文转载至 http://blog.csdn.net/allison162004/article/details/38758265 自动引用计数(ARC)是编译器的一个特色,提供了Objective- ...

  2. gulp安装教程

    1.安装nodejs并选装cnpm: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.全局安装gulp: cnpm i ...

  3. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. config相关操作(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/04/16/3025315.html,这是一个专题,感觉比较好,有空可以看与一下 System.Con ...

  5. POJ 2092 Grandpa is Famous【水---找出现第二多的数】

    链接: http://poj.org/problem?id=2092 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  6. hash是什么?

    最近读关于php内核的资料,发现php中 在实现变量以及数据类型的实现中大量使用哈希算法,并且非常细致做出了很多优秀的细节设计.比如:在 zend.hash.h 中 static inline ulo ...

  7. 遇到IIS configuration error错误的可以看看,不一定是权限问题

    最近接手了别人的一个 DOT NET项目,编译.调试一切都OK(心里暗暗高兴),发布吧,结果放到服务器上一运行出现Configuration Error错误,提示:“Access to the pat ...

  8. rtmp播放器

    rtmp测试地址: rtmp://live.hkstv.hk.lxdns.com/live 有的时候连接不上,不是很流畅 参考: 1,simplest flashmedia example http: ...

  9. C#与数据库连接简单测试

    效果展示   数据库代码 create database OneDb go USE OneDb; GO CREATE TABLE classify --分类表 ( id ,), name ) not ...

  10. python基础13 ---函数模块3(正则表达式)

    正则表达式 一.正则表达式的本质 1.正则表达式的本质(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的 ...