Gym 100650H Two Ends DFS+记忆化搜索
2015-08-18 20:22
295 查看
[b]Problem H: Two Ends[/b]
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.
[b]InputOutput[/b]
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.
[b]OutputSample Input[/b]
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.
[b]Sample Input[/b]
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0
[b]Sample ouput[/b]
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暴力
代码
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.
[b]InputOutput[/b]
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.
[b]OutputSample Input[/b]
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.
[b]Sample Input[/b]
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0
[b]Sample ouput[/b]
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=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //*************************************** int dp[1004][1004]; int n,a[1001]; int dfs(int l,int r) { if(dp[l][r]!=0){return dp[l][r];} if(r==l+1){ return dp[l][r]=abs(a[l]-a[r]); } else { int ans1,ans2; if(a[l+1]>=a[r]) ans1=dfs(l+2,r)+a[l]-a[l+1]; else ans1=dfs(l+1,r-1)+a[l]-a[r]; if(a[l]>=a[r-1]) ans2=dfs(l+1,r-1)+a[r]-a[l]; else ans2=dfs(l,r-2)+a[r]-a[r-1]; return dp[l][r]=max(ans1,ans2); } } int main() { int oo=1; while(scanf("%d",&n)!=EOF) {if(n==0)break; memset(dp,0,sizeof(dp)); int sum=0; for(int i=1;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(1,n)); /// cout<<ans-sum+ans<<endl; } return 0; }
相关文章推荐
- 第二集 监督学习的应用—梯度下降
- android 动画url 地址总结
- 栈
- POJ-3358Period of an Infinite Binary Expansion
- 第二集 监督学习的应用—梯度下降
- uboot系列之-----顶层Makefile分析(一)
- 希尔排序
- 一个图灵API的调用
- 支持向量机之线性可分支持向量机(一)
- 2015 Multi-University Training Contest 8
- Linux ag命令
- hdu5399(2015多校9)--Too Simple
- Proxmox 服务器搭建全过程(纯手打,亲测可用)
- TinyXml 操作XML 常用操作 http://www.cnblogs.com/cy568searchx/p/3670400.html
- 用Gson解析复杂的天气Json数据
- 英语常用时态
- Android Api Demos登顶之路(三十五)Fragment-->Argument
- 【Linux探索之旅】第一部分第六课:Linux如何安装在虚拟机中
- 【Python】[面向对象编程] 访问限制,继承和多态
- 观察系统资源的几个命令