The main topic: there are two rows of items, each row has n, Qingjun and dog brother take turns from each row of each side to pick any item. Qingjun initiator, suppose the dog elder brother very smart, blue June of the value of the goods and the maximum of how much?
Analysis: Interval DP.
Status: Dp[x1][y1][x2][y2], which represents the maximum value that can be taken in the interval [x1, y1] and interval [x2, y2].
State transition equation: There are four kinds of cases, namely x1+1,y1-1,x2+1,y2-1. Obviously take four of the smallest one.
In the following code, a B array is used to represent the prefix and the two rows.
#include <cstdio>#include <algorithm>#include <cstring>using namespace STD;intN, a[ -], b[ -];intd[ -][ -][ -][ -], v[ -][ -][ -][ -];intDP (intL1,intR1,intL2,intR2) {if(V[L1][R1][L2][R2])returnD[L1][R1][L2][R2]; V[L1][R1][L2][R2] =1;intsum =0, ans =0;if(L1 <= R1) sum + = A[r1]-a[l1-1];if(L2 <= R2) sum + = B[r2]-b[l2-1];if(L1 <= R1) {ans = max (ans, SUM-DP (l1+1, R1, L2, R2)); ans = max (ans, SUM-DP (L1, r1-1, L2, R2)); }if(L2 <= R2) {ans = max (ans, SUM-DP (L1, R1, l2+1, R2)); ans = max (ans, SUM-DP (L1, R1, L2, r2-1)); }returnD[L1][R1][L2][R2] = ans;}intMain () { while(scanf("%d", &n)! = EOF) {memset(d,0,sizeof(d));memset(V,0,sizeof(v)); for(inti =1; I <= N; i++) {inttmpscanf("%d", &tmp); A[i] = a[i-1] + tmp; } for(inti =1; I <= N; i++) {inttmpscanf("%d", &tmp); B[i] = b[i-1] + tmp; }printf("%d\n", DP (1N1, n)); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Meow Haha village provocation