题目链接地址:http://soj.me/1176

题目大意:两头取数。第一个人随机取,第二个人用贪婪算法(每次都取大的),求两人取数在第一个人赢的情况下的最大分差。使用贪婪算法时,如果左右两边相等,取左边的。

核心算法:动态规划。

设数组arr[a][b]是在数列区间[a,b]上的最大分差。

递推公式:

1.第一个人取左边的数arr[a]

if(arr[a+1] >=  arr[b])  point1 = input[a] - input[a+1] + dp(a+2,b);

else point1 = input[a] - input[b] + dp(a+1, b-1);

2.第一个人取右边的数arr[b]

if (input[a] >= input[b-1]) point2 = input[b] - input[a] + dp(a+1,b-1);

else point2 = input[b] - input[b-1] + dp(a,b-2);

arr[a][b] = point1 > point2 ? point1 : point2;

代码:

 // 动态规划!
#include <stdio.h> #define N 1005
#define ENDLESS 100000 int arr[N][N];
int input[N]; int dp(int a, int b) {
if (arr[a][b] != ENDLESS)
return arr[a][b]; if (a > b)
return ; // 第一个人取左边的数,第二个人贪婪算法
int point1;
if (input[a+] >= input[b]) {
point1 = input[a] - input[a+] + dp(a+,b);
} else {
point1 = input[a] - input[b] + dp(a+, b-);
} // 第一个人取右边的数,第二个人贪婪算法
int point2;
if (input[a] >= input[b-]) {
point2 = input[b] - input[a] + dp(a+,b-);
} else {
point2 = input[b] - input[b-] + dp(a,b-);
} arr[a][b] = point1 > point2 ? point1 : point2; return arr[a][b];
} int main() {
int n;
int count = ;
while (scanf("%d", &n) != EOF) {
if (n == )
break; for (int i = ; i < n; i++)
scanf("%d", &input[i]); for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
arr[i][j] = ENDLESS; printf("In game %d, the greedy strategy might lose by as many as %d points.\n", ++count, dp(,n-));
}
return ;
}

1176. Two Ends的更多相关文章

  1. sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

    Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...

  2. BZOJ 1176: [Balkan2007]Mokia

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2012  Solved: 896[Submit][St ...

  3. 【DP】HDU 1176

    HDU 1176 免费馅饼 题意:中文题目不解释. 思路:因为是从中间出发所以思路卡了许久,还在之前做了道HIHO入门的题.能想到的点,从时间思考,然后初始化1s的时候,4,5,6,的数值要特别赋值. ...

  4. 【BZOJ 1176】【Balkan 2007】Mokia

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 整体二分的例题 把每个询问拆成四个询问,整体二分里x坐标递增,按x坐标扫的时候用树状数组维护y ...

  5. 关于cout<<ends你不知道的那些事

    关于ends是C++中比较基础的一个东西,但是可能不是每个人都能够清楚的理解这是个什么东西,我就经历了这么一个过程,写出来让大家看看,有什么理解的不对的地方欢迎拍砖. 今天以前我对ends的理解是:输 ...

  6. HDU 1176免费馅饼 DP数塔问题转化

    L - 免费馅饼 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. Gym 100650H Two Ends DFS+记忆化搜索

    Problem H: Two EndsIn the two-player game “Two Ends”, an even number of cards is laid out in a row. ...

  8. HDU 1176 免费馅饼(记忆化搜索)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. poj 1176 Party Lamps

    http://poj.org/problem?id=1176 Party Lamps Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

随机推荐

  1. 微信小程序之threejs全景

    最近在开发小程序,身心疲惫,原因是功能和app相同,我裂开了. 各种封装组件,各种写页面,不过有个好处是以前写的h5拿来改一下标签,基本上还是ok的,就剩下最后几个功能,其中就有一个VR全景功能. 移 ...

  2. netbeans生成的maven工程没有web.xml文件 如何新建

    使用netbeans生成的maven工程没有web.xml 需要自己新建 步骤: 下一步,完成

  3. oracle习题-简单查询

    题一 1 实现将已知表中的数据插入到另一个表中 学生表:stu1 向表中插入两条数据   学生信息表2:stuinfo 将stu1表中的两条数据导入到stuinfo表中,执行下列语句 此时查看一下st ...

  4. 2019-8-31-Developing-Universal-Windows-Apps-开发UWA应用-问答

    title author date CreateTime categories Developing Universal Windows Apps 开发UWA应用 问答 lindexi 2019-08 ...

  5. SQL Sever实验三 视图与数据更新

    一. 实验目的 1.掌握创建视图的 SQL 语句,数据更新的 SQL 语句. 2.了解使用创建视图向导创建视图的方法. 3.掌握使用 SQL 创建视图的方法,使用 SQL 更新数据的方法. 二. 实验 ...

  6. jquery源码学习(一)——jquery结构概述以及如何合适的暴露全局变量

    jQuery 源码学习是对js的能力提升很有帮助的一个方法,废话不说,我们来开始学习啦 我们学习的源码是jquery-2.0.3已经不支持IE6,7,8了,因为可以少学很多hack和兼容的方法. jq ...

  7. pyenv虚拟环境管理python多版本和软件库

    可能大家在日常工作中会遇到这么个问题,现在基本的linux系统都是自带老版本的python2.7.x版本,我又不想用老版本,但直接升级可能会出问题,或是依赖老版本的程序就运行不了,有没办法能安装3.x ...

  8. OpenLayers添加地图标记

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  9. php 简单加密解密

    <?php namespace App\Service; /* * @link http://kodcloud.com/ * @author warlee | e-mail:kodcloud@q ...

  10. fedora安装mod_python

    3.1 Installing mod_python To install mod_python, we simply run: yum install mod_python 3.2 Configuri ...