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还是不敏感啊,虽然 ...
随机推荐
- Qt 向word中插入文字(使用QAxWidget和QAxObject)
pro 文件中要加入 CONFIG += qaxcontainer 2. main.cpp #include <QApplication> #include <QAxWidget&g ...
- PageHeap,调试Heap问题的工具
<Windows用户态程序高效排错>第二章主要介绍用户态调试相关的知识和工具.本文主要讲了PageHeap,调试Heap问题的工具. AD:51CTO学院:IT精品课程在线看! 2.4.2 ...
- ios王云鹤--iPhone中,点击换行,键盘消失。
1.要在声明文件中实现 UItextFieldDelegate 协议 2.在实现文件中实现 UItextFieldDelegate 协议 中键盘消失的方法即: -(BOOL)textFieldShou ...
- 介绍一个C++奇巧淫技
你能实现这样一个函数吗: MyType type; HisType htype; serialize_3(11, type, htype); serialize_4(type, hty ...
- CentOS上解压ZIP乱码的解决办法
今天在学校做实验需要解压缩一些范例程序,我准备在我的电脑上把这个实验做完,所以就把文件copy到我的CentOS里面去了. 但是万万没想到``````解压缩的时候悲剧了,用unzip解压出来一大堆乱码 ...
- zend framework将zip格式的压缩文件导入并解压到指定文件
html代码 <pre class="php" name="code"><fieldset> <legend>批量导入学生照 ...
- 【微信公众平台开发】公布动态新闻好帮手UEditor富文本
因为微信要做公布动态新闻.那就须要富文本. 上网搜索有非常多这样的插件,比方CKEditor.KindEditor等:最后看到百度一款开源的UEditor.官网打开,风格设计就吸引住了自己.所以就选U ...
- Python内置函数str()和repr()
内建函数str()和repr() (representation.表达,表示)或反引號操作符(``)能够方便地以字符串的方式获取对象的内容.类型.数值属性等信息. str()函数得到的字符串可读性好( ...
- H3C S5120交换机ACL应用的问题
下午在S5120上ACL的时候发现不起作用,ACL如下: acl number 3001 name deny-wan-to-lan-vpn rule 0 deny ip source 10.3.0.0 ...
- 首次启动优美新手指引tip
在开发商业应用时候,用户第一次进入app,有种无从下手的感觉,我们作为开发人员要提供可用户一些指引, 这些指引不能让用户看着唐突,要舒服的展示给用户,带着用户愉快的使用我们的app. 怎么让用户舒服呢 ...