Gym 100650H Two Ends DFS+记忆化搜索
Problem H: Two Ends
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.
InputOutput
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.
OutputSample Input
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 ouput
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.
题意:
给你一个序列,A和B轮流可以来选掉序列两端的一个值,B总是选两个里面最大的,而A按最佳方案选;问你AB的最大分差是多少;
题解:
dp[l][r]记录l->r的最大分差,进行DFS暴力
代码
//зїеп:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************
int dp[][];
int n,a[];
int dfs(int l,int r)
{
if(dp[l][r]!=){return dp[l][r];}
if(r==l+){
return dp[l][r]=abs(a[l]-a[r]);
}
else {
int ans1,ans2;
if(a[l+]>=a[r])
ans1=dfs(l+,r)+a[l]-a[l+];
else ans1=dfs(l+,r-)+a[l]-a[r];
if(a[l]>=a[r-])
ans2=dfs(l+,r-)+a[r]-a[l];
else ans2=dfs(l,r-)+a[r]-a[r-];
return dp[l][r]=max(ans1,ans2);
} }
int main()
{
int oo=;
while(scanf("%d",&n)!=EOF)
{if(n==)break;
memset(dp,,sizeof(dp));
int sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
} printf("In game %d, the greedy strategy might lose by as many as %d points.\n",oo++,dfs(,n));
/// cout<<ans-sum+ans<<endl;
}
return ;
}
Gym 100650H Two Ends DFS+记忆化搜索的更多相关文章
- 不要62 hdu 2089 dfs记忆化搜索
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1078(dfs记忆化搜索)
题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- 8636 跳格子(dfs+记忆化搜索)
8636 跳格子 该题有题解 时间限制:2457MS 内存限制:1000K提交次数:139 通过次数:46 题型: 编程题 语言: G++;GCC Description 地上有一个n*m 的数 ...
- 【每日dp】 Gym - 101889E Enigma 数位dp 记忆化搜索
题意:给你一个长度为1000的串以及一个数n 让你将串中的‘?’填上数字 使得该串是n的倍数而且最小(没有前导零) 题解:dp,令dp[len][mod]为是否出现过 填到第len位,余数为mod 的 ...
- poj1088-滑雪 【dfs 记忆化搜索】
http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 79806 ...
随机推荐
- struts返回json
<param name="includeProperties"> </param> 这个属性表示要包含进JSON数据中的数据.<param name= ...
- linux读写ntfs
frankly speaking, i hope to get a higher salary. yours frankly= yours sincerely = sincerely yours =y ...
- CentOS6.5安装iftop
iftop这个小工具是Linux和unix下的top命令升级版,功能相对较强,界面易懂.今天安装了CentOS6.5的最新版,装个小工具检查下系统运行性能. 官网:http://www.ex-parr ...
- Zhulina 的高分子刷理论
高分子刷的解析平均场理论有两种表述方式.一个是MWC理论(Macromolecules 1988, 21, 2610-2619),另外一个就是Zhulina和Birshtein这两位俄罗斯老太太的理论 ...
- cocos2dx 安卓编译问题收集
问题: 新的cocos2d-x 2.2.5 在使用Eclipse的安卓NDK 9 的编译器进行编译的时候,问题提示如下: [armeabi] Compile++ thumb: cocos_extens ...
- linux 客户端 Socket 非阻塞connect编程
开发测试环境:虚拟机CentOS,windows网络调试助手 非阻塞模式有3种用途 1.三次握手同时做其他的处理.connect要花一个往返时间完成,从几毫秒的局域网到几百 ...
- [BZOJ1202][HNOI2005]狡猾的商人
[BZOJ1202][HNOI2005]狡猾的商人 试题描述 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i= ...
- 【SpringMVC】SpringMVC系列8之Servlet API 作为入参
8.Servlet API 作为入参 8.1.概述 MVC 的 Handler 方法可以接受哪些 ServletAPI 类型的参数: HttpServletRequest HttpServletRes ...
- python安装问题
安装MinGW之后 出现.. 解决方案 ================
- css 中的度量单位
px 相对长度单位.像素(Pixel). 像素是相对于显示器屏幕分辨率而言的.譬如,WONDOWS的用户所使用的分辨率一般是96像素/英寸.而MAC的用户所使用的分辨率一般是72像素/英寸. em 相 ...