jerry

jerry

Title Description

As we all know, Jerry the mouse is a very clever mouse.

It can be calculated to Jerry wise subtraction 64 signed integer numbers.

Now, Jerry wrote a formula of only non-negative integer numbers and addition and subtraction thereof. It wants to add to this equation legitimate brackets, so that the result of maximum expression. Same here, addition and subtraction operations priority, and we come into contact with in their daily lives as if no parentheses, the first operator on the left, and then count on the right.

For example, Equation  (1 + 2) + 3- (4-5) + 6 ( 1 + 2 ) + . 3 - ( . 4 - . 5 ) + 6 is legal, but  ) + 1 2 ( ) 1 + 2 (and  ( -) 1 + 2 ( - ) 1 + 2, and  (1) + 2 ( 1 ) + 2 is not legitimate.

Input Format

Subsequently, a total of  T T sets of data, each of the following format:

A first line integer  n- n-, the number represents a number.

The next line co-  2N. 1- 2 n- - . 1 symbol or negative integers, separated by spaces to form a formula.

Output Format

After his party an integer representing the add brackets, most likely the result of the equation.

Sample

Sample input 1

1
3
5 - 1 - 3

Sample output 1

7

Sample 1 Description

5-(1-3) = 7

Sample input 2

1
4
1 - 1 - 1 - 3

Sample output 2

4

Sample 2 Description

1- (1 - 1 - 3) = 4

Sample 3

Issued an additional sample of a large document.

Data range and tips

Test point number n ranges Special nature
1 n \ 3 n 3 no
2,3 n \ 10 n 1 0 T \ 10 T 1 0
4,5 n \ 100 n 1 0 0 T \ 100 T 1 0 0
6,7 n \ the 1000 n 1 0 0 0 T \ 100 T 1 0 0
8,9,10 \sum {n} \le 2 \times 10^5n2×105 no

For all the data,  n-\ Le 10 ^. 5, \ SUM {n-} \ Le 2 \ Times 10 ^. 5, 2 \ Le n- n- . 1 0 . 5 , [Sigma n- 2 × . 1 0 . 5 , 2 n-, formula figures appearing in  [0,10 ^ 9] [ 0 , . 1 0 9 ] within the interval.

source

CSP-S 2019 in a simulation Changsha 2

 


Solution

 

So that F [i] [j] i represents the number of front, front left parenthesis of the j-th maximum.
At one point you can add a left parenthesis, delete a left parenthesis, or no change. (Equivalent to delete the two do not delete, add two ... think Shane)
Then it is n ^ 2 of the
Tell solution to a problem I need only two brackets, so he O (N) of the
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 1000006
#define ll long long
#define inf 1e16
#define _(d) while(d(isdigit(ch=getchar())))
using namespace std;
int T,n;
ll f[maxn][3],a[maxn],g[maxn];
ll R(){
    int F=1,v;char ch;_(!)if(ch=='-')F=0;v=(ch^48);
    _()v=(v<<1)+(v<<3)+(ch^48);return F?v:-v;
}
void work(){
    n=R();
    for(int i=1;i<=n;i++)a[i]=R();
    for(int i=0;i<=n;i++)
    for(int j=0;j<=2;j++)f[i][j]=-inf;
    g[0]=1;for(int i=1;i<=n;i++)g[i]=-g[i-1];
    f[0][0]=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=2;j++){
            int v=g[j]*a[i];
            if(j>0&&a[i-1]<0)f[i][j]=max(f[i][j],f[i-1][j-1]+g[j]*a[i]);
            f[i][j]=max(f[i][j],f[i-1][j]+g[j]*a[i]);
            if(j<2)f[i][j]=max(f[i][j],f[i-1][j+1]+g[j]*a[i]);
        }
    }
    ll ans=-inf;
    for(int x=0;x<=2;x++)ans=max(ans,f[n][x]);
    printf("%lld\n",ans);
}
int main(){
    for(scanf("%d",&T);T--;work());
    return 0;
}
View Code

 

 
 

Guess you like

Origin www.cnblogs.com/liankewei/p/11850039.html