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
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Sample Output

In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 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(记忆化)的更多相关文章

  1. POJ 1088 滑雪(记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description ...

  2. POJ 1088 滑雪 DFS 记忆化搜索

    http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...

  3. POJ 1088 滑雪【记忆化搜索】

    题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...

  4. poj 1088 滑雪_记忆化搜索

    题意:略 直接用记忆化搜索就行了 #include<cstdio> #include<iostream> using namespace std; int n,m; int m ...

  5. POJ 3176-Cow Bowling(DP||记忆化搜索)

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14210   Accepted: 9432 Desc ...

  6. poj棋盘分割(记忆化)

    http://poj.org/problem?id=1191 黑书上P116 想了挺久 没想出来 想推出一公式来着 退不出来.. 想偏了  正解:递归 #include <iostream> ...

  7. POJ 1088 滑雪 【记忆化搜索经典】

    题目链接:http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:  ...

  8. poj 1163 The Triangle 记忆化搜索

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44998   Accepted: 27175 De ...

  9. HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)

    题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...

随机推荐

  1. 与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室

    原文:与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...

  2. 关于Thread类中三个interrupt方法的研究与学习(转)

    先看三个方法原型: public void interrupt(): public boolean isInterrupted(): public static boolean interrupted ...

  3. jquery学习之AJAX

    1,关于AJAX的简单介绍 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准 ...

  4. 表空间的状态(二) - read/write

    表空间状态-READ ONLY.READ WRITE 1. 仅仅读表空间的主要用途就是为了消除对数据库大部分静态数据的备份和恢复的须要.Oracle不会更新仅仅读表空间爱你的文件.因此这部分文件能够存 ...

  5. 《转》VC++多线程编程

    原地址:http://www.cnblogs.com/wxfasdic/archive/2010/09/23/1833522.html 留个纪念,不错的总结.十个例子清晰列举啦多线程编程的奥妙.  V ...

  6. java基础---->摘要算法的介绍 (转)

    数据摘要算法是密码学算法中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数据签名.数据完整性校验等功能,由于其不可逆性,有时候会被用做敏感信息的加密.数据摘要算法也被称为哈希(Hash)算法. ...

  7. junit4同一时候測试多个測试类

    两个分别须要的測试类 TestSuit001 package com.test.junit; import org.junit.Test; public class TestSuit001 { @Te ...

  8. uva 11355(极角计算)

    传送门:Cool Points 题意:给一个圆心为原点的圆和一些线段,问所有线段两端点与圆心连线构成的角度总和占总360度的百分比. 分析:首先将所有线段的两端点变成极角,然后排序(范围[-PI,PI ...

  9. Kendo UI开发教程(14): Kendo MVVM 数据绑定(三) Click

    Click绑定可以把由ViewModel定义的方法不绑定到目标DOM的click事件.当点击目标DOM元素时触发ViewModel的对应方法.例如: 使用Click绑定 1 <div id=&q ...

  10. C语言中字符串如何转换为二进制、八进制、十进制、十六进制

    在C语言某个程序当中需要把文本16进制转换成对应的16进制数,比如字符串"0x1a"转换成10进制的26,可以用以下函数来实现 相关函数: atof, atoi, atol, st ...