传送门

题目大意

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

题解

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

    本文转载至 http://www.cocoachina.com/bbs/read.php?tid=93220&page=e&#a     持久化cookie ASIHTTPReques ...

  2. 关于angularjs的model的一些问题

    有的时候 在一些页面中 我们会需要用到弹出的模态框,这里主要是使用angularjs的uimodel. 页面效果如下: 首先我们需要在JS的controller中导入$uibModal模块. HTML ...

  3. C# RangeHelper

    /// <summary> /// Range Helper /// </summary> /// <typeparam name="T">&l ...

  4. Office 365系列(-)

    昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...

  5. http => https 升级

    准备证书 阿里云安全(云盾)-> CA证书服务,购买证书,个人测试的话可以使用免费的,期限1年. 购买证书后,把域名与证书进行绑定,提交审核,大概10分钟左右,正常情况下审核就可以通过.证书准备 ...

  6. java Filter的简单使用

    java web中的过滤器的简单使用.直接上代码.1.web.xml <?xml version="1.0" encoding="UTF-8"?> ...

  7. linux服务器最大连接数

    1 受内存限制 每个tcp连接是一个打开的socket文件,因此linux服务器的最大连接数受linux操作系统单个进程同时打开的最大文件数的限制. 这个限制本质上是对单个进程内存的限制. 查看进程最 ...

  8. 使用JavaScript定义一个微信小程序插件样例

    var wxTimer = new wxTimer({ beginTime: "00:00:20", complete: function () { wx.redirectTo({ ...

  9. ZOJ - 3761 Easy billiards 【并查集+DFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761 题意 在一个桌面上,给出一些球 如果在A球的某个方向的前方 ...

  10. HTTP请求解析--从一个请求开始

    先来看看一个http请求实例 请求行: POST /chapter17/user.html HTTP/1.1 请求头: Accept: application/json, text/javascrip ...