ARC006E Addition and Subtraction Hard

Link
首先我们发现左括号接在加号后面是没有意义的,所以左括号只能接在减号后面。
在左括号后的一段极长连续+x子序列的贡献系数都是(-1),而后面的不论是+x还是-x都可以达到绝对值。
这样构造出来可能存在两个括号连在一起的情况,进行等价变换即可。
不难发现最后的结果最多只有两层括号嵌套。
然后直接dp即可。

#include<cctype>
#include<cstdio>
#include<algorithm>
using i64=long long;
int fetc(){int c=getchar();while(c^'-'&&c^'+')c=getchar();return c;}
int read(){int x=0,c=getchar();while(isspace(c))c=getchar();while(isdigit(c))(x*=10)+=c&15,c=getchar();return x;}
int main()
{
    int n=read();i64 ans=read(),b=-1e18,c=-1e18;
    for(int x;n^1;--n) fetc()=='-'?(x=read(),c=b+x,ans=b=std::max(ans-x,c)):(x=read(),ans+=x,c+=x,b=std::max(b-x,c));
    printf("%lld",ans);
}
原文地址:https://www.cnblogs.com/cjoierShiina-Mashiro/p/12820108.html