# Include <iostream> <br/> using namespace STD; <br/> // There is an unordered positive integer array with 2n elements. It is required to divide it into two arrays whose number of elements is N, and make the sum of the two subarrays closer to the nearest. <Br/> int arr [] = {0, 1, 5, 7, 8, 9, 6, 3, 11, 20, 17}; <br/> const int n = 5; <br/> const int sum = 87; <br/> int split1 () <br/> {<br/> int DP [2 * n + 1] [n + 1] [sum/2 + 2]; </P> <p>/* <br/> dp (I, j, c) to represent the best (large) solution where J elements are obtained from the first I element and the sum of these J elements cannot exceed C. Here I> = J, c <= S <br/> state transition equation: <br/> // limits the number of items I do not get <br/> dp (I, j, c) = max {dp (I-1, J-1, C-A [I]) + A [I], dp (I-1, J, c)} <br/> dp (2n, n, sum/2 + 1) is the solution of the question. <Br/> */<br/> // initialization <br/> memset (DP, 0, sizeof (DP )); </P> <p> for (INT I = 1; I <= 2 * n; I ++) <br/> for (Int J = 1; j <= min (I, n); j ++) <br/> for (int s = sum/2 + 1; S> = arr [I]; s --) <br/> {<br/> DP [I] [J] [s] = max (DP [I-1] [J-1] [S-Arr [I] + arr [i], DP [I-1] [J] [s]); </P> <p >}</P> <p> // This is the final answer DP [2 * n] [N] [sum/2 + 1]. <br/> int I = 2 * n; <br/> Int J = N; <br/> int S = sum/2 + 1; <br/> while (I> 0) <br/> {<br/> If (DP [I] [J] [s] = DP [I-1] [J-1] [S-Arr [I] + arr [I]) // determine the state in which the State is derived <br/>{< br/> cout <arr [I] <""; // arr [I] <br/> j --; <br/> S-= arr [I]; <br/>}< br/> I --; <br/>}< br/> cout <Endl; <br/> return DP [2 * n] [N] [sum/2 + 1]; <br/>}</P> <p> int split2 () <br/>{< br/> int DP [n + 1] [sum/2 + 2]; // obtain n + 1 items. The total sum cannot exceed sum/2 + 2. The maximum value is </P> <p> memset (DP, 0, sizeof (DP); // The initial status is 0 <br/> for (INT I = 1; I <= 2 * n; I ++) <br/> for (Int J = 1; j <= min (I, n); j ++) <br/> for (INT S = sum/2 + 1; s> = arr [I]; s --) // 0 1 the size of the backpack ranges from large to small, saving space, that is, the space of the outermost layer <br/>{< br/> DP [J] [s] = max (DP [J-1] [S-Arr [I] + arr [I ], DP [J] [s]); <br/>}< br/> // space cannot be optimized if the optimal solution is required, <br/> return DP [N] [sum/2 + 1]; <br/>}</P> <p> int split3 () <br/> {<br/> int flag [n + 1] [sum/2 + 2]; // obtain n + 1 items, sum/2 + 2 is valid, that is, the sum of N + 1 items cannot be sum/2 + 2 <br/> memset (flag, 0, sizeof (FLAG); // both are invalid <br/> // note initialization <br/> flag [0] [0] = 1; // yes, 0 items, total 0, is legal </P> <p> for (INT I = 1; I <= 2 * n; I ++) <br/> for (Int J = 1; j <= min (I, n); j ++) <br/> for (INT S = sum/2 + 1; s> = arr [I]; s --) // from large to small, array missing one dimension <br/>{< br/> If (flag [J-1] [S-Arr [I]) <br/> flag [J] [s] = 1; <br/>}</P> <p> for (int s = sum/2 + 1; s> = 0; s --) <br/> If (flag [N] [s]) </P> <p> return S; </P> <p> // The space cannot be optimized if the optimal solution is required. <br/> return 0; <br/>}</P> <p> int main () <br/>{< br/> int S1 = split1 (); <br/> int S2 = split2 (); <br/> int S3 = split3 (); <br/> cout <"S1 =" <S1 <"S2 =" <S2 <"S3 =" <S3 <Endl; <br/> system ("pause"); <br/> return 0; <br/>}