Two Ends
来源:互联网 发布:为知笔记怎么样导出 编辑:程序博客网 时间:2024/04/28 05:25
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.
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.
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.
4 3 2 10 48 1 2 3 4 5 6 7 88 2 2 1 5 3 8 7 30
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.
#include <iostream>
#include <cstring>
using namespace std;
int num[1001];
int f[1001][1001];
int deep(int start,int end)
{
int ts=0;
int te=0;
if(end-start==1)
{
if(num[start]>num[end])
return f[start][end]=num[start];
else
return f[start][end]=num[end];
}
if(f[start][end]!=-1)
return f[start][end];
if(num[start+1]>=num[end])
ts=num[start]+deep(start+2,end);
else
ts=num[start]+deep(start+1,end-1);
if(num[end-1]>num[start])
te=num[end]+deep(start,end-2);
else
te=num[end]+deep(start+1,end-1);
if(ts>te)
return f[start][end]=ts;
else
return f[start][end]=te;
}
int main()
{
int n;
int count=0;
while(cin>>n)
{
if(n==0)
return 0;
memset(num,0,sizeof(num));
memset(f,-1,sizeof(f));
int sum=0;
count++;
for(int i=1; i<=n;i++)
{
cin>>num[i];
sum+=num[i];
}
int maxs=deep(1,n);
cout<<"In game "<<count<<", the greedy strategy might lose by as many as "<<2*maxs-sum<<" points."<<endl;
}
return 0;
}
- Two Ends
- POJ2738 Two Ends (DP)
- 1176. Two Ends
- 1176 Two Ends
- 1176. Two Ends
- poj 2738 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
- Sicily 1176. Two Ends
- Sicily 1176. Two Ends
- SOJ 1176. Two Ends
- Sicily 1176 Two Ends
- sicily 1176 two ends
- poj2738 Two Ends
- mysql替换回车、换行符
- 四川2名干部任前公示 王菲拟提名为广元市市长-四川-广元-任前公示
- android 框架 有很深的研究 价值
- 如何针对不同的人群返回这些人群需要的个性化搜索结果
- hdu - 4315 - Climbing the Hill - 博弈
- Two Ends
- 和菜鸟一起学linux之GCC内嵌汇编简单实例
- 利用进程管理程序
- penging order
- 黑马程序员________7k面试题之交通灯
- sshd服务配置
- 定点小数补码一位乘(Booth比较法)
- 琼州海峡越南籍沉船被打捞出水-琼州海峡-越南籍沉船-打捞出水
- ArcGIS for Android入门程序之DrawTool2.0