描述

Consider the following two-player game played with a sequence of N positive integers (2 <= N <= 100) laid onto a 1 x N game board. Player 1 starts the game. The players move alternately by selecting a number from either the left or the right end of the gameboar. That number is then deleted from the board, and its value is added to the score of the player who selected it. A player wins if his sum is greater than his opponents.

Write a program that implements the optimal strategy. The optimal strategy yields maximum points when playing against the "best possible" opponent. Your program must further implement an optimal strategy for player 2.

输入

Line 1:    N, the size of the board

Line 2-etc:    N integers in the range (1..200) that are the contents of the game board, from left to right

输出

Two space-separated integers on a line: the score of Player 1 followed by the score of Player 2.

样例输入

6
4 7 2 9
5 2

样例输出

18 11

题意

1*N的游戏盘,每个格子都有价值,玩家一次拿最左或最右,拿了删掉这个格子,问玩家1先拿,玩家2后拿,问俩人都最优可以拿多少分

题解

dp[i][j]=区间[i,j]先手-后手的差值

很容易推出dp[i][j]可由小区间得到

dp[i][j]=a[i]-dp[i+1][j];

dp[i][j]=a[j]-dp[i][j-1];

最终我们得到dp[1][n]为先手-后手的差值

设玩家1拿了V1,玩家2拿了V2

V1+V2=Σa;

V1-V2=dp[1][n];

求得

V1=(Σa+dp[1][n])/2;

V2=(Σa-dp[1][n])/2;

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 int main()
 5 {
 6     int n;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         int a[105],dp[105][105]={0},sum=0;
10         for(int i=1;i<=n;i++)
11             scanf("%d",&a[i]),sum+=a[i];
12         for(int len=1;len<=n;len++)
13             for(int i=1;i<=n-len+1;i++)
14             {
15                 int j=i+len-1;
16                 dp[i][j]=max(a[i]-dp[i+1][j],a[j]-dp[i][j-1]);
17             }
18         printf("%d %d\n",(sum+dp[1][n])/2,(sum-dp[1][n])/2);
19     }
20     return 0;
21 }

转载于:https://www.cnblogs.com/taozi1115402474/p/10269781.html

TZOJ 5101 A Game(区间DP)相关推荐

  1. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. 0x53. 动态规划 - 区间DP(习题详解 × 8)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 0x53. 动态规划 - 区间DP Problem A. 最优矩阵链乘 Problem B. ...

  3. UVA1626 括号序列 Brackets sequence(区间DP匹配括号,输出匹配方案)

    整理的算法模板合集: ACM模板 UVA1626 Brackets sequence 我们将正规括号序列定义如下: 空序列是正规括号序列. 如果 SSS 是一个正规括号序列,那么 (S) 和 [S] ...

  4. UVA10003 切木棍 Cutting Sticks(区间DP、细节)

    整理的算法模板合集: ACM模板 本题其实就是一个区间DP 的模板题,总长度为len,有n个切割点,也就是说能被切割成n+1段,所以左边界是0,有边界是n + 1,所以答案就是f[0][n + 1]. ...

  5. 【动态规划】区间DP - 最优矩阵链乘(另附POJ1651Multiplication Puzzle)

    最优矩阵链乘(动态规划) 一个n∗mn*mn∗m的矩阵由 nnn 行 mmm 列共 n∗mn*mn∗m 排列而成.两个矩阵A和B可以相乘当且仅当A的列数等于B的行数.一个nm的矩阵乘mp的矩阵,运算量 ...

  6. 【每日DP】day13、P3147 [USACO16OPEN]262144 (区间DP,2048游戏)难度⭐⭐⭐★

    P3147 [USACO16OPEN]262144 P 想到合并,自然就想到区间dp,一个被合成的数之前是一个区间,并且由两个数比它小 111 的区间合成.可麻烦的是,我们并不知道之前的两个区间长度各 ...

  7. poj1651(区间dp)

    题目连接:http://poj.org/problem?id=1651 题意:给出一组N个数,每次从中抽出一个数(第一和最后一个不能抽),该次的得分即为抽出的数与相邻两个数的乘积.直到只剩下首尾两个数 ...

  8. HDU 5115 Dire Wolf ——(区间DP)

    比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: 1 #i ...

  9. 编程之美2015资格赛 题目2 : 回文字符序列 [ 区间dp ]

    传送门 题目2 : 回文字符序列 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定字符串,求它的回文子序列个数.回文子序列反转字符顺序后仍然与原序列相同.例如字符串ab ...

最新文章

  1. Selenium之运行效果展示
  2. 清华袁洋:智能医疗不是让AI替代医生,而是……
  3. 《精通Unix下C语言与项目实践》读书笔记(16)
  4. KVM 介绍(4):I/O 设备直接分配和 SR-IOV [KVM PCI/PCIe Pass-Through SR-IOV]
  5. linux当卡片机的手机,卡片机不如手机?看完你就不这么想了
  6. WebService开发中SoapException的用法
  7. Oracle RAC学习笔记:基本概念及入门
  8. arduino 上传项目出错_Arduino多核编程:简单例子
  9. 创建线程时,需要创建的内容
  10. MemcacheQ 安装与使用
  11. 四元数与三维向量相乘运算法则
  12. 在指定时间执行定时任务
  13. 瑞典皇家理工学院计算机学什么,瑞典皇家理工大学学科设置是怎样的?
  14. 用Python制作生日蛋糕、生日快乐,生日祝福代码
  15. PHP 简易聊天室 利用redis的订阅发布功能
  16. Kali Linux学习笔记—Web渗透(1)
  17. 迄今为止最深刻分析家乐福的文章—从商业模式、公司制度、公司文化三方面
  18. Railway:怎么通过github来部署vue项目
  19. 学生信息管理系统(C语言版本+源码)
  20. 图嵌入Node2Vec安装

热门文章

  1. 结对项目-小学生四则运算系统网页版项目报告
  2. NGINX原理 之 SLAB分配机制(转)
  3. JavaSE第十五天20160823
  4. 第十章练习题----2
  5. 【语言处理与Python】2.5WordNet
  6. 十进制数转化为2进制后有多少个1
  7. 透明的WinForm窗体
  8. 我要认真学Git了 - Config
  9. CF888E Maximum Subsequence(meet in the middle)
  10. Docker 常用命令备忘录