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 ...
随机推荐
- 记一次PHP 数组基本用法
以前不知道PHP数组可以这样叠加. $b = array( '2' => 'zhang', ); $a = array( ' => 'li' ) + $b; print_r($a); $b ...
- java.lang.IllegalStateException: 1 matchers expected, 5 recorded.
这是一个很神奇的错误. 常规的出错是因为在mock方法里,其中某一个或者几个参数使用了EasyMock.anyxx(),而其他的使用了具体的值. java.lang.IllegalStateExcep ...
- LINUX普通猫的拔号工具介绍
普通猫分为串口和PCI的,请查看 <关于网络设备概述 > 普通猫的拔号工具主要有kppp和wvdial:在Redhat/Fedora中,用system-config-network 或re ...
- 洛谷P1003 [NOIP2011提高组Day1T1]铺地毯
P1003 铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号 ...
- 洛谷P1029 最大公约数和最小公倍数问题 [2017年6月计划 数论02]
P1029 最大公约数和最小公倍数问题 题目描述 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1 ...
- PHP获取真实客户端的真实IP的方法
REMOTE_ADDR 是你的客户端跟你的服务器“握手”时候的IP.如果使用了“匿名代理”,REMOTE_ADDR将显示代理服务器的IP. HTTP_CLIENT_IP 是代理服务器发送的HTTP头. ...
- [洛谷P4141] 消失之物「背包DP」
暴力:暴力枚举少了哪个,下面套一个01背包 f[i][j]表示到了i物品,用了j容量的背包时的方案数,f[i][j]=f[i-1][j]+f[i-1][j-w[i]]O(n^3) 优化:不考虑消失的, ...
- 清除SQL数据库文本字段中的回车、换行符的方法
清除SQL数据库中文本字段的回车.换行符的方法 清除回车符: update tableName set columnName = rtrim(ltrim(replace(columnName ,cha ...
- Hdu 3887树状数组+模拟栈
题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 图文结合深入理解 JS 中的 this 值
图文结合深入理解 JS 中的 this 值 在 JS 中最常见的莫过于函数了,在函数(方法)中 this 的出现频率特别高,那么 this 到底是什么呢,今天就和大家一起学习总结一下 JS 中的 th ...