USACO 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 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 gameboar. 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


————————————————————————————————————
博弈问题,所以用记忆化搜索,搜索每一个可能的状态寻找2先手或者1先手可以得到的最大值
然后更新的时候用对方利益最小化来更新,然后就做完了
 1 /*
 2 ID: ivorysi
 3 PROG: game1
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <set>
12 #include <vector>
13 #include <string.h>
14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
18 #define inf 0x3f3f3f3f
19 #define MAXN 400005
20 #define ivorysi
21 #define mo 97797977
22 #define ha 974711
23 #define ba 47
24 #define fi first
25 #define se second
26 #define pii pair<int,int>
27 using namespace std;
28 typedef long long ll;
29 int n;
30 int a[105],sum[105];
31 int dp[105][105];
32 void init() {
33     scanf("%d",&n);
34     siji(i,1,n) {scanf("%d",&a[i]);sum[i]=sum[i-1]+a[i];}
35     siji(i,1,n) siji(j,1,n) dp[i][j]=-1;
36 }
37 int dfs(int k,int l) {
38     if(dp[k][l]!=-1) return dp[k][l];
39     if(l==1) {return dp[k][1]=a[k];}
40     int x=min(dfs(k,l-1),dfs(k+1,l-1));
41     dp[k][l]=sum[k+l-1]-sum[k-1]-x;
42     return dp[k][l];
43 }
44 void solve() {
45     init();
46     dfs(1,n);
47     printf("%d %d\n",dp[1][n],sum[n]-dp[1][n]);
48 }
49 int main(int argc, char const *argv[])
50 {
51 #ifdef ivorysi
52     freopen("game1.in","r",stdin);
53     freopen("game1.out","w",stdout);
54 #else
55     freopen("f1.in","r",stdin);
56 #endif
57     solve();
58 }

 

 
posted @ 2017-01-01 19:12  sigongzi  阅读(406)  评论(0编辑  收藏  举报