POJ 2738 Two Ends(记忆化)
来源:互联网 发布:fanuc数控系统a角编程 编辑:程序博客网 时间:2024/06/05 09:28
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.
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.
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 48 1 2 3 4 5 6 7 88 2 2 1 5 3 8 7 30
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
East Central North America 2005
上次比赛写了个记忆化,这次算是跪了。
题意:第二个人是贪心拿法,只能从两端拿,问最大的差值。
#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;}
1 1
- POJ 2738 Two Ends(记忆化)
- POJ 2738 Two Ends 记忆化搜索
- POJ 2738 Two Ends(记忆化搜索+dp)
- poj 2738 Two Ends
- poj 2738 Two Ends 区间dp
- sicily 1176 Two Ends dp(记忆化搜索)
- [Sicily 1176 Two Ends] 动态规划 记忆化搜索
- Two Ends
- POJ2738 Two Ends (DP)
- 1176. Two Ends
- 1176 Two Ends
- 1176. Two Ends
- soj 1176. Two Ends
- Sicily 1176. Two Ends
- Sicily 1008 two ends
- sicily 1176.Two ends
- [sicily]1176. Two Ends
- 1176. Two Ends
- PHP代码加密
- [备忘-推荐]稳定好用的天气API
- HDOJ 1421 搬寝室 (DP)
- android4.0 avd模拟器的默认路径修改默认AVD的路径一般为系统用户目录下,WIN7下为C:\Users\Administrator\
- 交通灯系统
- POJ 2738 Two Ends(记忆化)
- 海康、大华IpCamera RTSP地址和格式
- Java的HashSet的对象遍历
- IOS-基础笔记之UIButton
- Spring与Hibernate Struts2等框架的整合
- JavaWeb学习日记2014-08-12
- diff和patch 打补丁
- 设计模式--工厂方法模式
- 数据结构与算法问题 二叉树的建立与遍历(含非递归)