IOI'96 - Day 1
Consider the following two-player game played with a sequence of N positive integers (2 <= N <= 100) laid onto a 1 x N game board. Player 1 starts the game. The players move alternately by selecting a number from either the left or the right end of the gameboard. That number is then deleted from the board, and its value is added to the score of the player who selected it. A player wins if his sum is greater than his opponents.
Write a program that implements the optimal strategy. The optimal strategy yields maximum points when playing against the "best possible" opponent. Your program must further implement an optimal strategy for player 2.
PROGRAM NAME: game1
INPUT FORMAT
Line 1: | N, the size of the board |
Line 2-etc: | N integers in the range (1..200) that are the contents of the game board, from left to right |
SAMPLE INPUT (file game1.in)
6 4 7 2 9 5 2
OUTPUT FORMAT
Two space-separated integers on a line: the score of Player 1 followed by the score of Player 2.SAMPLE OUTPUT (file game1.out)
18 11
"""
ID : mcdonne1
LANG : PYTHON
TASK : game1
"""
fin = open ('game1.in', 'r')
fout = open ('game1.out', 'w')
num = [0 for i in range (110)]
tot = [0 for i in range (110)]
f = [[0 for i in range (110)] for j in range (110)]
r = fin.readline()
n = int(r)
cnt = 0
while cnt < n :
r = fin.readline().split()
for i in range (len(r)) :
cnt += 1
num[cnt] = int(r[i])
f[cnt][cnt] = num[cnt]
tot[cnt] = tot[cnt - 1] + num[cnt]
for i in range (n)[::-1]:
for j in range (i + 1, n + 1) :
f[i][j] = max (tot[j] - tot[i] - f[i + 1][j] + num[i], tot[j - 1] - tot[i - 1] - f[i][j - 1] + num[j])
fout.write (str(f[1][n]) + ' ' + str(tot[n] - f[1][n]) + '\n')
fin.close()
fout.close()