Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4597
Question meaning:
Give two heaps of data, A and B, each person can only take the first or last number of each heap, the best performance of both, ask a to start, the number and maximum number he can get.
Solution:
Match search, or DP.
As soon as I got this question, I knew it was a memory-based search, but the status was transferred and I had not been very clear about it for a long time. Finally, I had a problem for a few hours because I was wrong about the water question .... Finally, there is no end...
At the beginning, DP is always made into a five-dimensional model, and the last one indicates who starts first. However, this transfer cannot be directly added, because there is another person behind it.
Finally, I heard the idea of the peak and peak, because there are only two people. When DFS is used, a parameter is maintained to indicate the sum of the remaining sum, and sum is used to subtract the maximum value of another person, which is obtained by myself! Orz.
This type of DP does little. Do more later.
DP [a] [B] [C] [d] indicates a ~ from the first push ~ B. The second-dimensional C ~ D. Select the maximum value.
PS: status transfer does not have to be +, -- minus.
Code:
# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define INF 0x1f1f1f1f # define PI ACOs (-1.0) # define ll _ int64 # define lson L, M, (RT <1) # define rson m + 1, R, (RT <1) | 1 # pragma comment (linker, "/Stack: 1024000000,1024000000") using namespa Ce STD; // freopen ("data. in "," r ", stdin); // freopen (" data. out "," W ", stdout); # define maxn 22int DP [maxn] [maxn] [maxn] [maxn]; int AA [maxn], BB [maxn], n; int DFS (int A, int B, int C, int D, int sum) {If (DP [a] [B] [C] [d]! =-1) return DP [a] [B] [C] [d]; If (A> B & C> D) // {DP [a] [B] [C] [d] = 0; return 0;} int res = 0; if (a <= B) {res = max (Res, Sum-DFS (a + 1, B, c, d, Sum-aa [a]); // current candidate a res = max (Res, Sum-DFS (A, B-1, c, d, Sum-aa [B]); // current candidate B} If (C <= d) {res = max (Res, Sum-DFS (A, B, C + 1, D, sum-BB [c]); // the current candidate C res = max (Res, Sum-DFS (a, B, c, D-1, sum-BB [d]); // current candidate d} DP [a] [B] [C] [d] = res; return res;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N); int sum = 0; for (INT I = 1; I <= N; I ++) scanf ("% d", & aa [I]), sum + = AA [I]; for (INT I = 1; I <= N; I ++) scanf ("% d", & BB [I]), sum + = BB [I]; memset (DP,-1, sizeof (DP); // initialize DFS (1, n, 1, n, sum); printf ("% d \ n ", DP [1] [N] [1] [N]);} return 0 ;}