20191024 csp-s simulation T2 (interval dp)

T2 jerry

2.1 Title Description
As we all know, Jerry Mouse is a very smart mouse. Jerry is smart enough to compute the addition and subtraction of 64 bit signed integer numbers. Now, Jerry wrote down a formula that consisted of only nonnegative integers and plus minus signs. It wants to add legal parentheses to the expression to maximize the result of the expression. Here, the priority of addition and subtraction is the same as that we encounter in our daily life. When there is no bracket, first calculate the left and then the right. For example, the formula ((1 + 2) + 3 (45)) + 6 ((1 + 2) + 3 (45)) + 6 ((1 + 2) + 3 (45)) + 6 is legal, but) 1 + 2 () 1 + 2 () 1 + 2 (and () 1 + 2 and (1) + 2 () 1 + 2 and (1) + 2 () 1 + 2 and (1) + 2 are illegal.
2.2 input description
The first line is an integer TTT representing the number of data groups.
Next, there are TTT group data. The format of each group is as follows:
The first line is an integer nnn, which represents the number of numbers.
The next line consists of 2n − 12n-12n − 1 symbols or non negative integers, forming an expression separated by spaces.
2.3 output description
An integer in a row represents the maximum possible result of the formula after parenthesis is added.
2.4 input samples & output samples
#1 input
1
3
5 - 1 - 3
#1 output
7
#2 input
2 4
1 - 1 - 1 - 3
1 - 1 - 1 + 3 4
#2 output
4
4
2.5 example description
For the first example:
5−(1−3)=75-(1-3)=75−(1−3)=7
For the second example:
1−(1−1−3)=41-(1-1-3)=41−(1−1−3)=4
1−(1−(1+3))=41-(1-(1+3))=41−(1−(1+3))=4
2.6 data range
For all data, n ≤ 105, Σ n ≤ 2 × 105, 2 ≤ N.N \ Leq 10 ^ 5, sum n \ Leq 2 \ times 10 ^ 5, 2 \ Leq N.N ≤ 105, Σ n ≤ 2 × 105, 2 ≤ n.

Train of thought:
It can be found that:
1. Only parentheses before negative numbers have an effect on the answer.
2. The maximum value can be constructed by adding two layers of brackets at most.
——>Certification:
If s [i] < 0s [i] \ LT 0s [i] < 0 and parentheses are added before iii, the value must be negative for the sum of consecutive positive numbers s[i+1] − s[j]s[i+1]-s[j]s[i+1] − s[j] after iii. For the first negative number s[j+1]s[j+1]s[j+1] s[j+1] after iii, add brackets in front of it to the second negative number s[k]s[k]s[k] after iii. It is not difficult to find that the numbers from j+1 to kj+1 to kj+1 to K are all positive when summing. This is true for all subsequent numbers and can be taken as positive.

First, all the continuous positive numbers are combined, and at most one positive number is separated between the two adjacent negative numbers. Then the suffix and sumsum are calculated, and dp is obtained from the back to the front.
Equation of state transition:
if(s[i]≥0)dp[i]=dp[i+1]+s[i];if(s[i]\ge0) dp[i]=dp[i+1]+s[i];if(s[i]≥0)dp[i]=dp[i+1]+s[i];
if(s[i]<0){if(s[i]\lt0)\lbraceif(s[i]<0){
if(s[i+1]<0)dp[i]=max(dp[i+1]+s[i],s[i]+sum[i+1]);if(s[i+1]\lt0) dp[i]=max(dp[i+1]+s[i],s[i]+sum[i+1]);if(s[i+1]<0)dp[i]=max(dp[i+1]+s[i],s[i]+sum[i+1]);
if(s[i+1]≥0)dp[i]=max(dp[i+1]+s[i],s[i]−s[i+1]+sum[i+2]);if(s[i+1]\ge0) dp[i]=max(dp[i+1]+s[i],s[i]-s[i+1]+sum[i+2]);if(s[i+1]≥0)dp[i]=max(dp[i+1]+s[i],s[i]−s[i+1]+sum[i+2]);
}\rbrace}

Code:

#include<bits/stdc++.h>
using namespace std;
#define in Read()
#define re register

inline char cha(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline long long in{
	long long s=0,f=1;char x;
	for(x=cha();!isdigit(x);x=cha())	if(x=='-')	f=-1;
	for( ;isdigit(x);x=cha())	s=(s<<1)+(s<<3)+(x&15);
	return s*f;
}

inline void out(long long x){
	if(x<0){
		putchar('-');
		x=-x;
	}	
	if(x/10)	out(x);
	putchar(x%10|48);
}

const long long A=2e5+5;
const long long INF=-1e18;
long long t,n;
char ch;
long long p[A];
long long dp[A];
long long a[A],tot,sum[A];

inline void scan(){
	n=in;
	p[1]=in;
	for(re int i=2;i<=n;++i){
		while(ch!='-'&&ch!='+')	ch=cha();
		p[i]=in*((ch=='+')?1:-1);
		ch='0';
	}
}

inline void work(){
	for(re int i=tot;i>=1;--i){
		dp[i]=dp[i+1]+a[i];
		if(a[i]<0){
			if(a[i+1]<=0)	dp[i]=max(dp[i],a[i]+sum[i+1]);
			if(a[i+1]>0)	dp[i]=max(dp[i],a[i]-a[i+1]+sum[i+2]);
		}
	}
	printf("%lld\n",dp[1]);
}

inline void clean(){
	memset(p,0,sizeof(p));
	memset(a,0,sizeof(a));tot=0;
	memset(sum,0,sizeof(sum));
	memset(dp,0,sizeof(dp));
}

inline void prepare(){
	long long all=0;
	for(re int i=1;i<=n;++i){
		if(p[i]>=0)	all+=p[i];
		if(p[i]<0){
			if(all){
				a[++tot]=all;
				all=0;
			}
			a[++tot]=p[i];
		}
	}
	if(all)	a[++tot]=all;
	for(re int i=tot;i>=1;--i)
		sum[i]=sum[i+1]+abs(a[i]);
}

signed main(){
	//freopen("jerry.in","r",stdin);
	//freopen("jerry.out","w",stdout);
	t=in;
	while(t--){
		clean();
		scan();
		prepare();
		work();
	}
	return 0;
}

Posted by steveDD on Thu, 24 Oct 2019 07:11:56 -0700