【hihocoder 1388】【区间DP】A Game【给出一个数列,两人每次从数列的头尾取数,小ho 先取,小hi每次都采取最优策略,问小ho最终能取得的数的总和最大是多少】
来源:互联网 发布:apache mpm 配置 编辑:程序博客网 时间:2024/05/19 16:48
传送门:http://hihocoder.com/problemset/problem/1338
题意:小hi和小ho玩游戏,给出一个数列,两人每次从数列的头尾取数,小ho 先取,小hi每次都采取最优策略,问小ho最终能取得的数的总和最大是多少。
思路:
区间动规
dp[i][l]表示当前某个人以最优策略在i为开头,l为长度的区间上执先手选数的最大答案,
转移方程为dp[i][l] = max(sum[i + l - 1] - sum[i - 1] - dp[i][l - 1], sum[i + l - 1] - sum[i - 1] - dp[i + 1][l - 1])(小ho取数组尾的数,小ho取数组头的数)
然后用前缀和优化一下复杂度为O(n^2)
代码:
#include <iostream>#include <stdio.h>using namespace std;#define rep(i,k,n) for(int i=k;i<=n;i++)template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num);}const int N=1e3+10;int n, a[N], sum[N], dp[N][N];int main(){ read(n); rep(i, 1, n) read(a[i]), sum[i] = sum[i - 1] + a[i], dp[i][1] = a[i]; for(int l = 2; l <= n; l++){ for(int i = 1; i + l - 1 <= n; i++){ dp[i][l] = max(sum[i + l - 1] - sum[i - 1] - dp[i][l - 1], sum[i + l - 1] - sum[i - 1] - dp[i + 1][l - 1]); } } printf("%d\n", dp[1][n]); return 0;}
描述:
#1338 : A Game
- 样例输入
4-1 0 100 2
- 样例输出
99
描述
Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.
Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy.
输入
The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)
The second line contains N integers A1, A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000)
输出
Output the maximum score Little Ho can get.
- 【hihocoder 1388】【区间DP】A Game【给出一个数列,两人每次从数列的头尾取数,小ho 先取,小hi每次都采取最优策略,问小ho最终能取得的数的总和最大是多少】
- 有一串数字,A、B两个人分别依次从两端取一个数,直到取完所有数,取得和最大者胜利;A、B都使用最优策略的情况下,求A、B最终的和;
- [HihoCoder]#1357 : 小Ho的防护盾
- hihoCoder 1473 小Ho的强迫症(加法取模)
- 【HPU 1415: 小ho的01串】& 尺取
- hihoCoder 1114 小Hi小Ho的惊天大作战:扫雷·一
- hihocoder 1114_小Hi小Ho的惊天大作战:扫雷·一
- hihocoder #1114 : 小Hi小Ho的惊天大作战:扫雷·一
- hihocoder #1114 : 小Hi小Ho的惊天大作战:扫雷·一
- 【hihoCoder】#1120 : 小Hi小Ho的惊天大作战:扫雷·三
- hihoCoder #1114 : 小Hi小Ho的惊天大作战:扫雷·一
- Hihocoder-1545 小Hi和小Ho的对弈游戏(博弈)
- hiho1505 : 小Hi和小Ho的礼物描述
- #1505 : 小Hi和小Ho的礼物
- hihocode 1505 : 小Hi和小Ho的礼物
- 小ho的01串
- 小ho的01串
- 小ho的01串
- DirectDraw编程
- 长期失眠怎么办?
- linux-基础 unit7
- PHP判断PC端还是手机端
- Java开发中的23种设计模式详解
- 【hihocoder 1388】【区间DP】A Game【给出一个数列,两人每次从数列的头尾取数,小ho 先取,小hi每次都采取最优策略,问小ho最终能取得的数的总和最大是多少】
- web前台的学习
- 15个Docker基本命令及用法
- Java NIO 系列教程
- Windows批处理 - 给自己写个库(2. 获取格式化的日期和时间)
- 幂等性 个人理解及应用
- Job for postfix.service failed. See 'systemctl status postfix.service' and 'journalctl -xn' for deta
- Java web后台设计
- java.util.concurrent翻译----Executor框架--接口ExecutorService