POJ 2738 Two Ends(记忆化)
Description
In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add
up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest -- we'll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.) 3 2 10 4 You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes. Input
There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you
may assume that the sum of the numbers in the list does not exceed 1,000,000. Output
For each test case you should print one line of output of the form:
In game m, the greedy strategy might lose by as many as p points. where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player's score and second player's score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end. Sample Input 4 3 2 10 4 Sample Output In game 1, the greedy strategy might lose by as many as 7 points. Source |
上次比赛写了个记忆化,这次算是跪了。參照:点击打开链接
题意:第二个人是贪心拿法,仅仅能从两端拿,问最大的差值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int maxn=1100;
int dp[maxn][maxn];
int a[maxn],n;
int solve(int x,int y)
{
if(dp[x][y]!=-1)
return dp[x][y];
if(x+1==y)
return dp[x][y]=abs(a[x]-a[y]);
int sa,sb;
if(a[x+1]>=a[y])//第一个人选左端
sa=solve(x+2,y)+a[x]-a[x+1];
else
sa=solve(x+1,y-1)+a[x]-a[y];
if(a[x]<a[y-1])//第一个人选右端
sb=solve(x,y-2)+a[y]-a[y-1];
else
sb=solve(x+1,y-1)+a[y]-a[x];
return dp[x][y]=max(sa,sb);
} int main()
{
int l=0;
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
memset(dp,-1,sizeof(dp));
printf("In game %d, the greedy strategy might lose by as many as %d points.\n",++l,solve(0,n-1));
}
return 0;
}
POJ 2738 Two Ends(记忆化)的更多相关文章
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...
- POJ 1088 滑雪 DFS 记忆化搜索
http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...
- POJ 1088 滑雪【记忆化搜索】
题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...
- poj 1088 滑雪_记忆化搜索
题意:略 直接用记忆化搜索就行了 #include<cstdio> #include<iostream> using namespace std; int n,m; int m ...
- POJ 3176-Cow Bowling(DP||记忆化搜索)
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14210 Accepted: 9432 Desc ...
- poj棋盘分割(记忆化)
http://poj.org/problem?id=1191 黑书上P116 想了挺久 没想出来 想推出一公式来着 退不出来.. 想偏了 正解:递归 #include <iostream> ...
- POJ 1088 滑雪 【记忆化搜索经典】
题目链接:http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- poj 1163 The Triangle 记忆化搜索
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44998 Accepted: 27175 De ...
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...
随机推荐
- js下读取input中的value值
很多人(包括我),总想像以前操作js一样,读取到input中的值:document.getElementById('').value; 结果事实证明这样读到得是null. eval(document. ...
- [docker]coreOS与atomic对照
声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 摘自https://majo ...
- VS2010使用静态编译的qt库
Qt开发界面很方便,但发布程序就不那么方便了,你的把引用到的dll一起发布才行,要是能静态编译就好了,发布的时候只有一个exe多方便. 虽然以前为了方便,直接安装的qt-windows-opensou ...
- hdu 4034 Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...
- delphi 使用superobject实现jsonrpc的http远程调用 good
http://blog.csdn.net/earbao/article/details/46423167
- JEECG开源团队招募新成员 2014年
JEECG开源团队招募新成员 2014年 截止日期:2014-06-01 JEECG开源项目 是一款基于代码生成器的微云高速开发平台.提供企业高速开发和採用微信实现移动应用的解决方式.J ...
- 修改emlog后台登录路径的方法(转)
emlog后台登录地址的目录名称默认为admin,并且官方没有提供自定义后台登录入口名字的功能,这多少让我们觉得有些不安全,毕竟暴露一个网站的后台不是一件安全的事,今天就给您说下修改方法,增加一下网站 ...
- Liftoff Software | Next stop, innovation
Liftoff Software | Next stop, innovation Previous Next Gate One 1.1 Now Available Submitted by Dan M ...
- sql server2012附加的数据库问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM2NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- hdu2569(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2569 分析: f(n),n个珠子的合格数:a(n),n个珠子,最后2个相同的合格数:b(n),n个珠子 ...