Light OJ 1031 --- Easy Game (interval DP), oj1031 --- easy

Source: Internet
Author: User
Tags integer numbers

Light OJ 1031 --- Easy Game (interval DP), oj1031 --- easy

Question Link

Http://lightoj.com/volume_showproblem.php? Problem = 1031.

 

Description

You are playing a two player game. Initially there areNInteger numbers in an array and playerAAndBGet chance to take them alternatively. each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. he can take as your consecutive numbers as he wants during his time. the game ends when all numbers are taken from the array by the players. the point of each player is calculated by the summation of the numbers, which he has taken. each player tries to achieve more points from other. if both players play optimally and playerAStarts the game then how much more point can playerAGet than playerB?

Input

Input starts with an integerT (≤ 100), Denoting the number of test cases.

Each case contains a blank line and an integerN (1 ≤ N ≤ 100)Denoting the size of the array. The next line containsNSpace separated integers. You may assume that no number will contain more4Digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input Output for Sample Input

2

 

4

4-10-20 7

 

4

1 2 3 4

Case 1: 7

Case 2: 10

 

N numbers are arranged in one row. Now A and B take any number from both ends (at least one at A time) until all the numbers are obtained, how much is the sum of the numbers obtained by A greater than that of B?

Train of Thought: interval DP, dp [I] [j] represents interval I ~ The sum of the numbers obtained by j A is much larger than that obtained by B. Therefore, we can analyze the number of intervals I ~ J A first obtains sum [k]-sum [I-1] or sum [j]-sum [k] (can only be obtained from both ends), then the B takes, that is, for the range k + 1 ~ J and I ~ K DP is much larger than B, so the state transition equation is dp [I] [j] = max (dp [I] [j], max (sum [k]-sum [I-1]-dp [k + 1] [j], sum [j]-sum [k]-dp [I] [k]);

The Code is as follows:

# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace std; int sum [105]; int dp [105] [105]; /// how much is A bigger than B? Int main () {int T, Case = 1; int n; cin> T; while (T --) {sum [0] = 0; scanf ("% d ", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & sum [I]); sum [I] + = sum [I-1];} memset (dp, 0, sizeof (dp); for (int I = 1; I <= n; I ++) dp [I] [I] = sum [I]-sum [I-1]; for (int len = 1; len <n; len ++) {for (int I = 1; I <= n; I ++) {if (I + len> n) break; dp [I] [I + len] = sum [I + len]-sum [I-1]; for (int k = I; k <I + len; k ++) {dp [I] [I + len] = max (dp [I] [I + len], max (sum [k]-sum [I-1]-dp [k + 1] [I + len], sum [I + len]-sum [k]-dp [I] [k]) ;}} printf ("Case % d: % d \ n ", case ++, dp [1] [n]) ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.