usaco Chapter 3 section 3.3 A game

/*

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
符号及变量说明:
记num为数的序列,Num[i]为序列中第i个数.
记s(i,j)为序列中第i个数到第j个数的和.
记F(i,j)为序列中由i开始到第j个数按照游戏规则能取得到的最大值.

模型的建立:

注意如果该人是f[i][j],那么他必须让他的对手的值为f[i+1][j] , f[i][j-1] 中较小的一个。
由于后一个人按最佳策略取数,所以他必将取一个最大值s[i][j]-f(i+1,j)或s[i][j]-f(i,j-1),那么此时取数的人以后取得的数将是sum(i+1,j)-f(i+1,j)或是sum(i,j-1)-f(i,j-1),此时取数的人按最佳策略取数,他会取Qi或者Qj使得s(i+1,j)-f(i+1,j)+num[i]和s(i,j-1)-f(i,j-1)+num[j]二者能有较大值.这样从游戏的结束向游戏的开始推,可以得到总的最优解。

模型的求解:
显然,f(i,i)=sum(i,i)=num[i];这可以作为边界条件.

这个题卡了n久,世上无难事,只怕有心人。


*/

 

/*
ID: niepeng1
PROG: game1
LANG: C++
*/
#include <iostream>

using namespace std;
#define Max1 101
#define mmax(a,b) a>b?a:b

int s[Max1][Max1],num[Max1],f[Max1][Max1];
int main()
{
 FILE *in,*out;
 int N,i,j;
 in=fopen("game1.in","r");
 out=fopen("game1.out","w");

 fscanf(in,"%d",&N);
 for(i=0;i<N;i++)
 {
  fscanf(in,"%d",&num[i]);
  f[i][i]=num[i];
  s[i][i]=num[i];
 }
 for(i=N-2;i>=0;i--)
  for(j=i+1;j<N;j++)
  {
   s[i][j]=s[i][j-1]+num[j];
   f[i][j]=mmax(s[i][j] - f[i+1][j],s[i][j] -f[i][j-1]);

  }
 fprintf(out,"%d %d/n",f[0][N-1],s[0][N-1]-f[0][N-1]);

 fclose(in);fclose(out);
 return 0;
}

 

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.032 secs, 3004 KB]
   Test 2: TEST OK [0.000 secs, 3004 KB]
   Test 3: TEST OK [0.011 secs, 3004 KB]
   Test 4: TEST OK [0.000 secs, 3004 KB]
   Test 5: TEST OK [0.011 secs, 3004 KB]
   Test 6: TEST OK [0.000 secs, 3004 KB]
   Test 7: TEST OK [0.022 secs, 3004 KB]
   Test 8: TEST OK [0.011 secs, 3004 KB]
   Test 9: TEST OK [0.022 secs, 3004 KB]
   Test 10: TEST OK [0.000 secs, 3004 KB]
   Test 11: TEST OK [0.011 secs, 3004 KB]
   Test 12: TEST OK [0.011 secs, 3004 KB]
   Test 13: TEST OK [0.022 secs, 3004 KB]
   Test 14: TEST OK [0.022 secs, 3004 KB]
   Test 15: TEST OK [0.011 secs, 3004 KB]
   Test 16: TEST OK [0.011 secs, 3004 KB]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值