lightoj 1031 - Easy Game(区间DP)
来源:互联网 发布:c语言高级教程 编辑:程序博客网 时间:2024/04/30 12:45
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 Astarts 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 than4 digits.
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
给你一列数字,你可以取最左边连续的几个,或者最右边连续的几个,问你第一个人取得的数字和最大能比第二个大多少。
dp[i][j]表示区间i到j先手最多比后手多多少分,每一段都这么处理,那么dp[1][n]就是第一个人比第二个人多的分数。
就是找到一点k,使得dp[l][k]-dp[k+1][r]或者dp[k+1][r]-dp[l][k]最大。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int main(void){ int T,n,i,j,k,l; int sum[110]; int dp[110][110]; scanf("%d",&T); int cas = 1; while(T--) { scanf("%d",&n); sum[0] = 0; for(i=1;i<=n;i++) { int x; scanf("%d",&x); sum[i] = sum[i-1] + x; } for(l=1;l<=n;l++) { for(i=1;i+l-1<=n;i++) { j = i + l - 1; dp[i][j] = sum[j]-sum[i-1]; for(k=i;k<j;k++) { int t = max(sum[k] - sum[i-1] - dp[k+1][j],sum[j] - sum[k] - dp[i][k]); dp[i][j] = max(dp[i][j],t); } } } printf("Case %d: %d\n",cas++,dp[1][n]); } return 0;}
- LightOJ 1031 - Easy Game (区间dp)
- lightoj 1031 - Easy Game(区间DP)
- LightOJ - 1031 Easy Game(区间DP)
- LightOJ 1031 Easy Game(区间DP)
- LightOJ 1031 - Easy Game【区间dp】
- lightoj 1031 - Easy Game 【区间dp】
- lightoj 1031 - Easy Game 【区间dp】
- lightoj 1031 Easy Game (区间dp 博弈)
- LightOJ 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)
- Light OJ 1031 Easy Game 区间DP
- lightoj 1031 - Easy Game
- lightoj 1031 - Easy Game
- LightOJ 1031-Easy Game
- lightoj1031 - Easy Game【区间dp】
- vs2010 问题 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- MVC框架设计简介
- Python selenium —— 文件下载,不弹出窗口,直接下载到指定路径
- json解析工具
- linux yum命令
- lightoj 1031 - Easy Game(区间DP)
- 字典的快速赋值 setValuesForKeysWithDictionary
- github客户端使用简易教程
- quartus生成的各文件含义
- Android调用webservice返回数据为空
- hadoop学习过程中遇到的问题记录
- MVC4.0网站发布和部署到IIS7.0上的方法
- Android开发——回调(Callback)
- c++中CreateEvent函数