您的位置:首页 > 其它

USACO3.3.5--A Game

2013-03-01 14:16 357 查看
A Game
IOI'96 - Day 1
Consider the following two-player game played with a sequence of N positive integers (2 <= N <= 100) laid onto a 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 sequence. 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
题解:这个博弈问题可以用动态规划来解决。设sum[i]为序列s[1]...s[i]的和,f[i][j]表示先手玩家在序列i..j中获得的最大值。那么f[i][j]=sum[j]-sum[i-1]-min(f[i+1][j],f[i][j-1]),这个方程表示的是,如果先手玩家选取左边的元素s[i]或者右边的元素s[j],那么后手玩家肯定也会用最优策略选取f[i+1][j]或者f[i][j-1],我们只要使得后手玩家在序列i..j中获得的最大值最小即可。循环的时候注意i是从n递减的,因为我们要保证由飞f[i+1][j]和f[i][j-1]这两个子问题得出f[i][j]。(第一次提交的时候i是从1递增到n,果断WA了。。。以后记得认真考虑循环控制)。

View Code

/*
ID:spcjv51
PROG:game1
LANG:C
*/
#include<stdio.h>
#include<string.h>
#define MAXSN 100
long min(long a,long b)
{
return a<b?a:b;
}
int main(void)
{
freopen("game1.in","r",stdin);
freopen("game1.out","w",stdout);
long f[MAXSN+5][MAXSN+5],sum[MAXSN+5];
int s[MAXSN+5];
int i,j,n;
scanf("%d",&n);
sum[0]=0;
for(i=1; i<=n; i++)
{
scanf("%d",&s[i]);
sum[i]=sum[i-1]+s[i];
f[i][i]=s[i];
}
for(i=n; i>=1; i--)
for(j=i+1; j<=n; j++)
f[i][j]=sum[j]-sum[i-1]-min(f[i+1][j],f[i][j-1]);
printf("%ld %ld\n",f[1]
,sum
-f[1]
);
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: