lightoj1031 - Easy Game【区间dp】
来源:互联网 发布:月薪5000如何理财 知乎 编辑:程序博客网 时间:2024/04/30 09:33
You are playing a two player game. Initially there are n integer numbers in an array and player 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
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4digits.
Output
For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.
Sample Input
Output for Sample Input
2
4
4 -10 -20 7
4
1 2 3 4
Case 1: 7
Case 2: 10
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<list>#include<vector>using namespace std;const int maxn=110;int sum[maxn];int dp[maxn][maxn];int main(){int n,i,j,k,l,t,test=1;scanf("%d",&t);while(t--){scanf("%d",&n);memset(dp,0,sizeof(dp));for(i=1;i<=n;++i){scanf("%d",&dp[i][i]);sum[i]=sum[i-1]+dp[i][i];}for(l=1;l<=n;++l){for(i=1;i<=n-l;++i){j=i+l;dp[i][j]=sum[j]-sum[i-1];for(k=i;k<j;++k){dp[i][j]=max(dp[i][j],max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k]));/***********************第一个人取左半部分时省下的即把第二个人看做第一个人在区间k+1到j获得的最大差值用第一个人取左半部分的值减去dp[k+1][j]即为第一个人先去左边是获得的区间值 ***********************/}}}printf("Case %d: %d\n",test++,dp[1][n]);}return 0;}
- lightoj1031 - Easy Game【区间dp】
- LightOJ - 1031 Easy Game(区间DP)
- LightOJ 1031 - Easy Game (区间dp)
- LightOJ 1031 Easy Game(区间DP)
- Light OJ 1031 Easy Game (区间DP)
- LightOJ 1031 - Easy Game【区间dp】
- lightoj 1031 - Easy Game 【区间dp】
- lightoj 1031 - Easy Game 【区间dp】
- Light OJ 1031 Easy Game 区间DP
- lightoj 1031 Easy Game (区间dp 博弈)
- lightoj 1031 - Easy Game(区间DP)
- LightOJ 1031 Easy Game--区间dp
- lightoj 1031 - Easy Game 博弈类区间DP
- ZOJ_3791_An Easy Game(DP)
- Lightoj 1031 - Easy Game DP
- zoj3791(An Easy Game) DP
- [LightOJ 1031]Easy Game[DP]
- hihocoder A Game 区间dp
- Android位置服务--百度定位,获取自己的位置
- 【札记】读《暗访十年》有感
- 列表生成式
- Spark(1.2.0) Streaming 集成 Kafka 总结 [复制链接]
- 写两个线程,一个加,一个减
- lightoj1031 - Easy Game【区间dp】
- 2016随记
- HTMl5的sessionStorage和localStorage
- centos 升级内核到最新版本
- 关于c++包含空格的字符串输入
- Spark组件之Spark Streaming学习1--NetworkWordCount学习
- viewPager 实现banner图 自动轮播
- ios返回上两级页面
- Java IO:CharArrayWriter使用及源码分析