uva10891(sum游戏)区间dp
2016-03-08 19:15
253 查看
题目:
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
terminated by 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
4
4 -10 -20 7
4
1 2 3 4
0
Sample Output
7
10
题意:
给出n(n<=100)个数,有A,B两个人,两个人都可以从这些数的左右两个边界取任意多个数,A先取,如果两个都足够聪明,都是按照最优策略取数,那么最后A的分数减去B的分数是多少.
分析:
定义d(i,j),表示原始数列第i~j个元素组成的子序列,在双方都采取最优策略的情况下,先手得分的最大值。
状态转移时,我们需要枚举从左边取还是从右边取以及取多少个,这等于枚举给对方剩下怎样的子序列,
是(k,j) (i< k<=j,用0表示全部取完)还是(i,k)(i<=k< j,用0表示全部取完);
因此状态转移方程是:
sum[i][j]表示从i到j的和。
d(i,j)=sum[i][j]-min(d(i,k),d(k+1,j),…….,0)
ac代码:
ac代码2:
ac2还可以直接这样写
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
terminated by 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
4
4 -10 -20 7
4
1 2 3 4
0
Sample Output
7
10
题意:
给出n(n<=100)个数,有A,B两个人,两个人都可以从这些数的左右两个边界取任意多个数,A先取,如果两个都足够聪明,都是按照最优策略取数,那么最后A的分数减去B的分数是多少.
分析:
定义d(i,j),表示原始数列第i~j个元素组成的子序列,在双方都采取最优策略的情况下,先手得分的最大值。
状态转移时,我们需要枚举从左边取还是从右边取以及取多少个,这等于枚举给对方剩下怎样的子序列,
是(k,j) (i< k<=j,用0表示全部取完)还是(i,k)(i<=k< j,用0表示全部取完);
因此状态转移方程是:
sum[i][j]表示从i到j的和。
d(i,j)=sum[i][j]-min(d(i,k),d(k+1,j),…….,0)
ac代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int a[105],dp[105][105]; int main() { //freopen("test.txt","r",stdin); int n; while(~scanf("%d",&n)&&n) { a[0]=0; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { int num; scanf("%d",&num); a[i]=a[i-1]+num; } for(int k=1;k<=n;k++) { for(int i=1;i<=n-k+1;i++) { int mmin=0; for(int j=i+1;j<=i+k-1;j++) mmin=min(dp[j][i+k-1],mmin); for(int j=i;j<i+k-1;j++) mmin=min(dp[i][j],mmin); dp[i][i+k-1]=a[i+k-1]-a[i-1]-mmin; } } int ans=2*dp[1] -a ; printf("%d\n",ans); } return 0; }
ac代码2:
#include<iostream> #include<cstdio> using namespace std; const int maxn=105; const int inf=0x3f3f3f3f; int a[maxn],d[maxn][maxn],sum[maxn]; int main() { //freopen("in.txt","r",stdin); int n; while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); d[i][i]=a[i]; d[i][i-1]=0; sum[i]=sum[i-1]+a[i]; } for(int i=n-1;i>0;i--) { for(int j=i+1;j<=n;j++) { int mmin=inf; for(int k=i+1;k<=j+1;k++) mmin=min(mmin,d[k][j]); for(int k=j-1;k>=i-1;k--) mmin=min(mmin,d[i][k]); d[i][j]=sum[j]-sum[i-1]-mmin; } } printf("%d\n",2*d[1] -sum ); } }
ac2还可以直接这样写
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=105; const int inf=0x3f3f3f3f; int d[maxn][maxn],a[maxn],sum[maxn]; int main() { //freopen("in.txt","r",stdin); int n; while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } memset(d,0,sizeof(d)); for(int i=1;i<=n;i++) d[i][i]=a[i]; for(int i=n-1;i>0;i--) for(int j=i+1;j<=n;j++) { int mmin=inf; for(int k=i+1;k<=j;k++) mmin=min(mmin,d[k][j]); for(int k=i;k<j;k++) mmin=min(mmin,d[i][k]); mmin=min(mmin,0); //全部取完 d[i][j]=sum[j]-sum[i-1]-mmin; } int ans=2*d[1] -sum ; printf("%d\n",ans); } }
相关文章推荐
- 用redis实现支持优先级的消息队列
- 共享栈[数组]
- ubuntu 12.04 安装Docker 实战
- linux查看是否开启超线程
- Extjs RadioGroup中Radio的切换
- cat 显示文件 创建文件 合并文件
- HDFS架构理解
- SpannableString与SpannableStringBuilder
- 【HDU5637 BestCoder Round 74 (div1)B】【bfs预处理】Transform 15个值+17个2的幂数最小异或步数使得s变成t
- 【POJ 3114】Countries in War(Tarjan+Dijkstra)
- 堆排序
- transform 2
- Jlink接口的Jtag和SWD接口定义
- iOSDay03C语言循环结构
- 有关静态方法和普通方法的调用
- angular.element方法汇总以及AngularJS 动态添加元素和删除元素
- 【HDU5636 BestCoder Round 74 (div1)A】【floyd 最短路 复杂度考核】Shortest Path 一条链3条边1e6个询问的两点最短路
- 在mingw项目中引入googletest
- 第十章 内部类
- Codeforces 631A - Interview