UVA 10891 Game of Sum dp(记忆化搜索)
2013-08-07 10:08
323 查看
Problem E
Game of SumInput File: e.in
Output: Standard Output
This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or
right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation
of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?
Input
The input consists of a number of cases. Each case starts with a line specifying the integer n (0 < n ≤100), the number of elements in the array. After that, n numbers are given for the game. Input is terminatedby a line where n=0.
Output
For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.
Sample Input Output for Sample Input
4 4 -10 -20 7 4 1 2 3 4 0 | 710 |
思路:dp。dp[l][r]表示区间[l,r]之间A能得到的最大分数,状态转移方程为:dp[l][r]=sum[l][r]-min(dp[l+k][r],dp[l][r-k])
本题因为数据范围比较小,我于是用记忆化搜索来做的。写搜索总比dp写的顺……
AC代码:
#include<iostream> #include<cstdio> #include<cstring> #define INF 10000006 using namespace std; int f[105][105],a[105],n; int dfs(int l,int r){ if(l>r)return 0; if(f[l][r]<INF)return f[l][r]; for(int i=1;i<=r-l+1;i++){ int a=dfs(l,r-i); int b=dfs(l+i,r); f[l][r]=min(f[l][r],a<b?a:b); } f[l][r]=a[r]-a[l-1]-f[l][r]; return f[l][r]; } int main(){ int n; while(scanf("%d",&n),n){ for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) f[i][j]=INF; f[0][0]=a[0]=0; for(int i=1;i<=n;i++){ scanf("%d",a+i); f[i][i]=a[i]; a[i]+=a[i-1]; } printf("%d\n",2*dfs(1,n)-a ); //for(int i=1;i<=n;i++){ // for(int j=i;j<=n;j++){ // cout<<i<<' '<<j<<' '<<f[i][j]<<endl; // } //} } }
相关文章推荐
- UVA - 10891 Game of Sum(记忆化搜索 区间dp)
- UVA 10891 Game of Sum(区间DP)
- UVa 10891 Game of Sum(DP)
- Uva 10891 - Game of Sum ( 区间dp )
- UVA 10891 Game of Sum(区间DP)
- UVa 10891 Game of Sum / 记忆化搜索
- UVA 10891-Game of Sum(基础DP)
- UVA 10891 Game of Sum (区间DP)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum(区间DP)
- UVA - 10891 Game of Sum (区间DP)@
- UVa 10891 Game of Sum (区间DP&博弈)
- UVA 10891 Game of Sum(区间DP)
- Uva 10891 Game of Sum(区间博弈dp)
- UVA 10891 Game of Sum(经典区间dp)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 - Game of Sum(区间dp)
- uva10891 Game of Sum 博弈区间dp
- UVA 10891 Game of Sum(区间DP)