Luo Gu P1430 serial number taken

Face questions

    Chicken busy holiday really www, read a book and thought the car playing baseball one day is gone (of course, LOL & decadent chat time).

    P and universities must be big hit on the right ACM and the like, can only learn the w (Alexander)

    So OI is really only interested in the strike, and occasionally have time to mess things up, start with a simple question about the recovery even had the strength is also very dish w

 

    Well, not much gossip said.

    This question is seemingly appears LRJ's ​​Blue Book too, is equivalent to just get max score (score upper hand - FLAC) max, and because both the constant.

    This achieves a O (n ^ 3) of the DP, then we take the front and from the prefix or suffix, respectively, taken at optimized (see specific code comments), can be done O (n ^ 2) it!

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1005;

int dp,w,f[N][N],g[N][N],a[N],n,T;

/*  
    dp[i][j] means the MAX delta between the first and the second.
    
    f[i][j] = min{w[i][j]+dp[i][j],w[i+1][j]+dp[i+1][j],...,w[j][j]+dp[j][j]}
	g[i][j] is similar... (reverse)
	
	And there w[i][j] means the sum of a[k](k from i to j)
	
	Initially, dp[i][i] = a[i], f[i][i]=g[i][i]= 2*a[i].
	Except the initial, dp[i][j] = w[i][j] - min(f[i+1][j],g[i][j-1],0) 
	
	
	Finally the ans is (dp[1][n]+w[1][n])/2.
*/

inline void solve(){
	for(int i=1;i<=n;i++) f[i][i]=g[i][i]=2*a[i],a[i]+=a[i-1];
	
	for(int l=1;l<n;l++)
	    for(int i=1,j=i+l;j<=n;j++,i++){
	    	w=a[j]-a[i-1];
	    	dp=w-min(0,min(f[i+1][j],g[i][j-1]));
	    	f[i][j]=min(f[i+1][j],w+dp);
	    	g[i][j]=min(g[i][j-1],w+dp);
		}
	
	printf("%d\n",dp+a[n]>>1);
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",a+i);
		
		if(n==1) printf("%d\n",a[1]); else solve();
	}
	
	return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/JYYHH/p/11256538.html