您的位置:首页 > 其它

[USACO3.3.5]A Game

2017-07-31 22:14 176 查看
4000

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

/*
如果n是2的倍数,似乎就是个裸的贪心。
可是如果不是,似乎做不来了。。
贪心的想法 好像是错的。
但是可以用DP解!
不妨用dp[i,j]表示当前(i,j)这一段区间,先手-后手的得分
显然
dp[i,j]可以从dp[i+1,j]和dp[i,j-1]转移
dp[i,j] = max(a[i] - dp[i+1,j],a[j] - dp[i,j-1]);
预先得到最终先手+后手的得分S
解一个2元一次方程组——名字好有趣。
USER: Jack Chen [cqz15311]
TASK: game1
LANG: C++

Compiling...
Compile: OK

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

All tests OK.
YOUR PROGRAM ('game1') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

*/
/*
ID:cqz15311
LANG:C++
PROG:game1
*/
#include<cstdio>
#include<algorithm>
using namespace std;
int n,dp[105][105],a[105],sum;
int main(){
freopen("game1.in","r",stdin);
freopen("game1.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum = sum + a[i];
dp[i][i] = a[i];
}
for (int len=2;len<=n;len++){
for (int i=1;i<=n-len+1;i++){
int j = i + len - 1;
dp[i][j] = max(a[i] - dp[i+1][j],a[j] - dp[i][j-1]);
}
}
printf("%d %d\n",(dp[1]
+ sum) / 2,(sum - dp[1]
) / 2);
fclose(stdin);
fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态规划