Light OJ 1031---Easy Game(区间DP)
2018-06-17 23:50:07来源:未知 阅读 ()
题目链接
http://lightoj.com/volume_showproblem.php?problem=1031
Description
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 4 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 |
题意:有n个数排成一行,现在A和B两人从两端取任意个数(每次至少取一个),直到取完所有的数,A先取,求A取得数的和比B大多少?
思路:区间DP,dp[i][j] 表示区间i~j A取得数的和比B大多少,那么可以这样分析:对于区间i~j A先取sum[k]-sum[i-1]或sum[j]-sum[k](只能从两端取),然后该B取了,即对于区间k+1~j和i~k DP转换为B比A大多少了,所以状态转移方程为 dp[i][j]=max(dp[i][j],max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k]));
代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int sum[105]; int dp[105][105]; ///A比B大多少? int main() { int T,Case=1; int n; cin>>T; while(T--) { sum[0]=0; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&sum[i]); sum[i]+=sum[i-1]; } memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) dp[i][i]=sum[i]-sum[i-1]; for(int len=1;len<n;len++) { for(int i=1;i<=n;i++) { if(i+len>n) break; dp[i][i+len]=sum[i+len]-sum[i-1]; for(int k=i;k<i+len;k++) { dp[i][i+len]=max(dp[i][i+len],max(sum[k]-sum[i-1]-dp[k+1][i+len],sum[i+len]-sum[k]-dp[i][k])); } } } printf("Case %d: %d\n",Case++,dp[1][n]); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- CF1215DTicketGame——(博弈) 2020-04-25
- AtCoder agc007_d Shik and Game 2020-02-08
- 《游戏引擎构架Game Engine Architecture》略读笔记 2019-09-23
- kuangbin专题 专题一 简单搜索 Fire Game FZU - 2150 2019-08-16
- D - Dice Game (BFS) 2019-02-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash