1176. Two Ends
题目链接地址: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的更多相关文章
- 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 ...
- BZOJ 1176: [Balkan2007]Mokia
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2012 Solved: 896[Submit][St ...
- 【DP】HDU 1176
HDU 1176 免费馅饼 题意:中文题目不解释. 思路:因为是从中间出发所以思路卡了许久,还在之前做了道HIHO入门的题.能想到的点,从时间思考,然后初始化1s的时候,4,5,6,的数值要特别赋值. ...
- 【BZOJ 1176】【Balkan 2007】Mokia
http://www.lydsy.com/JudgeOnline/problem.php?id=1176 整体二分的例题 把每个询问拆成四个询问,整体二分里x坐标递增,按x坐标扫的时候用树状数组维护y ...
- 关于cout<<ends你不知道的那些事
关于ends是C++中比较基础的一个东西,但是可能不是每个人都能够清楚的理解这是个什么东西,我就经历了这么一个过程,写出来让大家看看,有什么理解的不对的地方欢迎拍砖. 今天以前我对ends的理解是:输 ...
- HDU 1176免费馅饼 DP数塔问题转化
L - 免费馅饼 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 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. ...
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- poj 1176 Party Lamps
http://poj.org/problem?id=1176 Party Lamps Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
随机推荐
- TZ_05_Spring_转账事务基于xml的开发
事务:通过接口的动态代理加强AccountService 实现转账的事务 ApplicationContext.xml <?xml version="1.0" encodin ...
- 使用jquery封装一个可以复用的提示框
首先在html中 <div class="backcap"> <div class="diolag"> <div class=&q ...
- 前言-使用Eclipse创建SpringBoot项目
1.首先我们需要安装STS插件:Help--> Eclipse Marketplace 2. 然后 File-->New--->Spring Starter Project 3.下 ...
- CentOS7 安装 Nginx 1.12.1
安装准备: nginx 依赖的一些 lib 库: yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel ...
- BOM头导致 php获取数据出现问题
1.BOM头导致 php获取数据出现问题 2.access-token 存redis 解决方案:存redis 一.下载redis驱动 二.配置redis
- 模拟3题解 T3建造游乐园
T3建造游乐园 这题的关键是推式子 i个点中,有g[i]个方案是度为偶数但不一定连通那么就要减去不合法的设已有j个合法,其个数为f[j],剩下i-j个的方案数是g[i-j]选出来一个固定的点在合法的j ...
- c++ 进制转换函数
转自:https://blog.csdn.net/wangjunchengno2/article/details/78690248 strtol 函数: 它的功能是将一个任意1-36进制数转化为10进 ...
- spark应用程序引用别的jar包
第一种方式 操作:将第三方jar文件打包到最终形成的spark应用程序jar文件中 应用场景:第三方jar文件比较小,应用的地方比较少 第二种方式 操作:使用spark-submit提交命令的参数: ...
- notepad++ 退出后关闭所有文档(关闭“记住最后打开的文件”)
旧版本: 设置->首选项->其他->取消勾选Remember current session for next launch 新版本: 设置->首选项->备份->取 ...
- C#截取字符串的方法小结(转)
1.单个字符分隔用split截取 string str = "GT123_1"; string[] strArray = str.Split('_'); //输出:sArray[0 ...