Problem Links:
uva10891,Problem:
Problem E
Game of Sum
Input File: e.in
Game of Sum
Input File: e.in
Output: Standard Output
This is a two player game. Initially there are n integer numbers in an array and players A and B get 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 many 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 player A starts the game then how much more point can player A get than player B?
Input
The input consists of a number of cases. Each case starts with a line specifying the integer n (0 < n ≤100), the number of elements in the array. After that, n numbers are given for the game. Input is terminated by a line where n=0.
Output
For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.
Sample Input Output for Sample Input
4
4 -10 -20 7
4
1 2 3 4
0
|
7
10
|
Problem setter: Syed Monowar Hossain
Special Thanks: Derek Kisman, Mohammad Sajjad Hossain
Source Code:
import java.io.BufferedReader;import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* cost[x][y][z] represents the maximum sum of array from x to y for user z(0
* for user A, 1 for user B).(y>=x)
*
* cost[x][y][z] = sum from x to y (inclusive) subtract the cost[x][index][z^1]
* or cost[index][y][z^1];
*/
/**
* @author antonio081014
* @time: Mar 14, 2012, 3:35:03 PM
*/
class Main {
public static final int SIZE = 110;
// Mark if (x,y,z) is visited or not;
public boolean[][][] mark;
// The maximum sum of array from x to y for user z. (y>=x)
public int[][][] cost;
public int[] data;
public int N;
public static void main(String[] args) throws Exception {
Main main = new Main();
main.run();
System.exit(0);
}
public void run() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
cost = new int[SIZE + 1][SIZE + 1][2];
mark = new boolean[SIZE + 1][SIZE + 1][2];
data = new int[SIZE + 1];
while ((N = Integer.parseInt(br.readLine())) != 0) {
StringTokenizer tz = new StringTokenizer(br.readLine());
for (int i = 1; i <= N; i++) {
data[i] = data[i - 1] + Integer.parseInt(tz.nextToken());
}
init();
System.out.println(2 * solve(1, N, 0) - data[N]);
}
}
public void init() {
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++)
for (int z = 0; z < 2; z++) {
cost[i][j][z] = Integer.MIN_VALUE;
mark[i][j][z] = false;
}
}
public int solve(int x, int y, int z) {
if (mark[x][y][z])
return cost[x][y][z];
if (x > y || x == 0 || y == 0) {
mark[x][y][z] = true;
return cost[x][y][z] = 0;
}
// The most remarkable part of this solution;
for (int tmp = 1; tmp <= y - x + 1; tmp++) {
cost[x][y][z] = Math.max(cost[x][y][z], data[y] - data[x - 1]
- solve(x + tmp, y, z ^ 1));
cost[x][y][z] = Math.max(cost[x][y][z], data[y] - data[x - 1]
- solve(x, y - tmp, z ^ 1));
}
mark[x][y][z] = true;
return cost[x][y][z];
}
}
No comments :
Post a Comment