light oj 1031
来源:互联网 发布:路由器mac地址查询 编辑:程序博客网 时间:2024/06/03 22:53
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
2
4
4 -10 -20 7
4
1 2 3 4
Sample Output
Case 1: 7
Case 2: 10
题意:有一个序列,A和B两个人可以从两端取数,每个人至少取一个数 ,至多取完所有数,A先取,问最后A取得的数的和比B取得的数的和最大大多少;
区间dp,dp[ i ][ j ] 表示取完区间 i 到 j A取得的数之和比B取得的数之和最多多少, 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 <string.h>using namespace std;const int maxn=105,inf=0x3f3f3f3f;int a[maxn],sum[maxn],dp[maxn][maxn];int main(){ int t,T=0,n; cin>>t; while(t--) { cin>>n; memset(dp,-0x3f,sizeof(dp)); sum[0]=0; for(int i=1;i<=n;i++) { cin>>dp[i][i]; sum[i]=sum[i-1]+dp[i][i]; } for(int i=n-1;i>=1;i--) { for(int j=i+1;j<=n;j++) { for(int k=i;k<=j;k++) { if(k==j) { dp[i][j]=max(dp[i][j],sum[j]-sum[i-1]); continue; } else { dp[i][j]=max(dp[i][j],max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k] ) ); } } } } cout<<"Case "<<++T<<": "<<dp[1][n]<<endl; } return 0;}
- light oj 1031
- Light OJ 1031 区间dp
- 区间DP :Light OJ 1031
- light oj
- light oj
- Light OJ
- Light OJ
- light oj 1031 区间dp+博弈
- Light OJ 1031 Easy Game (区间DP)
- light oj 1031(区间dp入门)
- Light OJ 1031 Easy Game 区间DP
- light oj 1031(区间dp+博弈)
- Light OJ 1000
- Light OJ 1001
- Light OJ 1008
- Light OJ 1022
- Light OJ 1015
- Light OJ 1042
- vtk实战(四十七)——写入tif格式
- allocator类
- LeetCode 005 Longest Palindromic Substring *
- Program3_F
- BZOJ4562 [Haoi2016]食物链
- light oj 1031
- tomcat闪退,手动无法启动,针对tomcat6,7,8
- Thinking In Java 笔记
- Segmentation fault
- 拷贝构造与重载运算符
- 机器学习\数据挖掘学习日记 20160430
- javascript---学习笔记
- 使用asset catalog管理图片资源
- 图解cygwin下进行Linux 内核hack失败记