Title Link: http://lightoj.com/volume_showproblem.php?problem=1031
Title Description:
Give a series, two people take turns to fetch the number, finish. Can take a lot of numbers at a time, but only from the first or the end of the beginning to take a number of consecutive. Q. What is the maximum value of the last two?
Interval DP;
I've been reading a few ladder reports before I figured it out, and now I think it fits the requirements of dynamic planning.
D (I, j) represents the maximum value that a number of people can fetch in the array I to J, and then enumerates the split points in the middle,
ans = max (ans, sum[k]-sum[i-1]-d (k+1, J));
ans = max (ans, sum[j]-sum[k-1]-d (i, k-1));
Ways to adopt and memorize searches
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <cstdio>6#include <vector>7#include <ctime>8#include <queue>9#include <list>Ten#include <Set> One#include <map> A using namespacestd; - #defineINF 0x3f3f3f3f -typedefLong LongLL; the - inta[ the], sum[ the], dp[ the][ the]; - intSolveintLintR) - { + if(Dp[l][r]! =-1*INF) - returnDp[l][r]; + intres = sum[r]-sum[l-1]; A for(inti = l; I <= R; i++) at { -res = max (res, sum[i]-sum[l-1]-solve (i+1, R)); -res = max (res, sum[r]-sum[i-1]-solve (L, I-1)); - } -DP[L][R] =Res; - returnRes; in } - intMain () to { + intt, N; -scanf"%d", &t); the for(intCA =1; CA <= T; ca++) * { $scanf"%d", &n);Panax Notoginsengsum[0] =0; - for(inti =1; I <= N; i++) the for(intj = i; J <= N; J + +) +DP[I][J] =-1*INF; A for(inti =1; I <= N; i++) the { +scanf"%d", &a[i]); -Dp[i][i] =A[i]; $Sum[i] = sum[i-1] +A[i]; $ } -printf"Case %d:%d\n", CA, Solve (1, N)); - } the return 0; -}View Code
Lightoj1031_ interval DP