Time limit : 2sec / Memory limit : 256MB

Score : 900 points

Problem Statement

Joisino has a formula consisting of N terms: A1 op1 A2  opN−1 AN. Here, Ai is an integer, and opi is an binary operator either + or -. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.

Constraints

  • 1≦N≦105
  • 1≦Ai≦109
  • opi is either + or -.

Input

The input is given from Standard Input in the following format:

  1. N
  2. A1 op1 A2 opN1 AN

Output

Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.


Sample Input 1

Copy
  1. 3
  2. 5 - 1 - 3

Sample Output 1

Copy
  1. 7

The maximum possible value is: 5−(1−3)=7.


Sample Input 2

Copy
  1. 5
  2. 1 - 2 + 3 - 4 + 5

Sample Output 2

Copy
  1. 5

The maximum possible value is: 1−(2+3−4)+5=5.


Sample Input 3

Copy
  1. 5
  2. 1 - 20 - 13 + 14 - 5

Sample Output 3

Copy
  1. 13

The maximum possible value is: 1−(20−(13+14)−5)=13.

题意:

给你一个只含有数字和加减号的字符串表达式,你可以加无数个括号,来改变运算顺序。

让你求出这个表达式能表达的最大的结果值。

思路:

首先我们要知道这个结论,

即一个表达式最多有效 括号最多是两层,因为只有在负号后面加括号才有作用,那么一个括号对括号里面的数值的影响是取负号一次,两层是取一部分是负,一部分是正(两层里的),如果有三层,那么第三层提取到最外边一层不会改变数值。

那么我们即可定义dp的状态

dp[i][j] 表示到第i个数时,第i个数后面有j个括号时,表示式的最大值。

那么我们来分析一下转移:

我们从i-1转移到i

当是‘+’时

我们可以在 dp[i-1][j] 后面直接加上 a[i] 即可转移到 dp[i][0]

在d[i-1][1] 括号里 加入a[i] ,贡献值为 - a[i] ,可以转移到dp[i][1]

dp[i-1][2] 最里层括号加入a[i] ,贡献值 为+ a[i] 可以转移到dp[i][2]

当是'-' 时

我们默认减号后面必须加括号,即使只包括自己  即这样的  - ( a[i] )

那么我们把负号后面的dp[i][0] = -inf ,即无穷小,不影响我们其他的运算。

我们可以在 dp[i-1][0/1 ]后面加上 -(a[i]) 即转移到 dp[i-1][1]

在dp[i-1][1]的括号里面加上 - (a[i] ),或者dp[i-1][2] 的外层括号里加上 - (a[i]) 

转移到dp[i][2] ,数值贡献时 +a[i]

这样就把全部的转移表示出来了,注意开ll。

细节见代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define rt return
  14. #define dll(x) scanf("%I64d",&x)
  15. #define xll(x) printf("%I64d\n",x)
  16. #define sz(a) int(a.size())
  17. #define all(a) a.begin(), a.end()
  18. #define rep(i,x,n) for(int i=x;i<n;i++)
  19. #define repd(i,x,n) for(int i=x;i<=n;i++)
  20. #define pii pair<int,int>
  21. #define pll pair<long long ,long long>
  22. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  23. #define MS0(X) memset((X), 0, sizeof((X)))
  24. #define MSC0(X) memset((X), '\0', sizeof((X)))
  25. #define pb push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. #define eps 1e-6
  30. #define gg(x) getInt(&x)
  31. #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
  32. using namespace std;
  33. typedef long long ll;
  34. ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  35. ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  36. ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
  37. inline void getInt(int* p);
  38. const int maxn=;
  39. // const int inf=0x3f3f3f3f;
  40. /*** TEMPLATE CODE * * STARTS HERE ***/
  41. int n;
  42. char op[maxn];
  43. int a[maxn];
  44. ll dp[maxn/][];
  45. const ll inf=(ll)(1e18);
  46.  
  47. int main()
  48. {
  49. //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  50. //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
  51. gbtb;
  52. cin>>n;
  53. cin>>a[];
  54. repd(i,,n)
  55. {
  56. cin>>op[i]>>a[i];
  57. }
  58. dp[][]=a[];
  59. dp[][]=dp[][]=-inf;
  60. repd(i,,n)
  61. {
  62. if(op[i]=='+')
  63. {
  64. dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]))+a[i];
  65. dp[i][]=dp[i-][]-a[i];
  66. dp[i][]=dp[i-][]+a[i];
  67. }else
  68. {
  69. dp[i][]=-inf;
  70. dp[i][]=max(dp[i-][],dp[i-][])-a[i];
  71. dp[i][]=max(dp[i-][],dp[i-][])+a[i];
  72. }
  73. }
  74. cout<<max(dp[n][],max(dp[n][],dp[n][]))<<endl;
  75.  
  76. return ;
  77.  
  78. }
  79.  
  80. inline void getInt(int* p) {
  81. char ch;
  82. do {
  83. ch = getchar();
  84. } while (ch == ' ' || ch == '\n');
  85. if (ch == '-') {
  86. *p = -(getchar() - '');
  87. while ((ch = getchar()) >= '' && ch <= '') {
  88. *p = *p * - ch + '';
  89. }
  90. }
  91. else {
  92. *p = ch - '';
  93. while ((ch = getchar()) >= '' && ch <= '') {
  94. *p = *p * + ch - '';
  95. }
  96. }
  97. }

AtCoder Regular Contest 066 E - Addition and Subtraction Hard (结论+DP)的更多相关文章

  1. AtCoder Regular Contest 066 F Contest with Drinks Hard

    题意: 你现在有n个题目可以做,第i个题目需要的时间为t[i],你要选择其中的若干题目去做.不妨令choose[i]表示第i个题目做不做.定义cost=∑(i<=n)∑(i<=j<= ...

  2. Atcoder Regular Contest 066 F genocide【JZOJ5451】

    题目 分析 \(s[i]\)表示a前缀和. 设\(f[i]\)表示做完了1~i的友谊颗粒的最优值(不一定选i),那么转移方程为 \[f[i]=max\{f[i-1],max\{f[j]-s[i]+s[ ...

  3. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  4. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  5. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  6. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  7. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  8. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

  9. AtCoder Regular Contest 102

    AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...

随机推荐

  1. Python的datetime与Decimal数据进行json序列化的简单说明

    我们在Python的json.JSONEncoder类中可以查看Python数据序列化为JSON格式的数据时数据类型的对应关系: class JSONEncoder(object): "&q ...

  2. HTTPS 证书制作及使用

    一 证书的制作 进入jdk/bin,使用keytools.exe制作证书. 1.创建keystore 创建一个别名为serverkeystore的证书,该证书存放在名为server.keystore的 ...

  3. 2018 icpc 沈阳

    https://codeforces.com/gym/101955 J 签到 #include<iostream> #include<cstring> #include< ...

  4. 五:flask-url_for使用详解

    from flask import url_for url_for(视图函数名):根据视图函数名指定url,只要视图函数不变,url随便变都不会影响 url_for源码: 示例视图,执行流程 带参数: ...

  5. Python学习之==>常用字符串方法

    1.常用字符串方法 a = '\n 字 符 串 \n\n' b = a.strip() # 默认去掉字符串两边的空格和换行符 c = a.lstrip() # 默认去掉字符串左边的空格和换行符 d = ...

  6. 【SpringMVC】---RequestMapping、Ant 路径、PathVariable 注解、HiddenHttpMethodFilter 过滤器、用 POJO 作为参数

    一.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  7. 【算法与数据结构】图的最小生成树 MST - Prim 算法

    Prim 算法属于贪心算法. #include <stdio.h> #define VERTEXNUM 7 #define INF 10000 typedef struct Graph { ...

  8. laravel 5.6 使用RabbitMQ作为消息中间件

    1.Composer安装laravel-queue-rabbitmqcomposer require vladimir-yuldashev/laravel-queue-rabbitmq2.在confi ...

  9. linux 进程间共享内存示例

    写入端: #include <iostream> #include <unistd.h> #include <stdlib.h> #include <stdi ...

  10. RabbitMQ使用(上)

    1. 说明 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的但是执行起来比较较耗时的地方, ...