| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1666 人关注过本帖
标题:Two Ends 求助
取消只看楼主 加入收藏
剑风曲
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-11-16
收藏
 问题点数:0 回复次数:5 
Two Ends 求助

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.


想了半天没什么思路~~大家帮偶想想,有什么思路~~~~

搜索更多相关主题的帖子: Ends Two 
2006-12-09 21:46
剑风曲
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-11-16
收藏
得分:0 
题目我大概翻译一下:
有2个人A,B做游戏,给出一组数,两个人轮流取数,要求,每次只能取第一个或者最后一个数.
A先取,然后B取,知道取完结束.B只会取第一个或者最后一个两者中的大的那一个.
用编程算法,计算出A最多能比B多出多少点数.
比如 3 2 10 4
A先取3,B只能取2和4中最大的,也就是取4,A取10,B取3,结果A比B多出3+10-(4+2)也就是7点.
输入:
首先输入一个数N,接下来有N个数组成的数组.当N=0时程序结束.
输出:
对于N个数组成的数组,A尽可能多的取得的比B多的最大点数;


输出格式就不翻译了,一看就明白的~~
2006-12-09 21:53
剑风曲
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-11-16
收藏
得分:0 

楼上的可能没分析清楚,B是一定取最大的,但是A不一定,就拿上面写的例子来说,
A先取,取的是3,那么B只能取4,A就能接着取到10,这样就很比B大7点.
如果A先取,去的是4的话,那么B接下来会去取10,结果是A输了.
题目要求我们寻找A取数的策略,使得A必须赢,而且需要赢最大的点数,也就是比B多出的点数最大.
对于B取的数,很简单就能实现,但是对于A怎么取数,就需要算法了.

2006-12-10 09:23
剑风曲
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-11-16
收藏
得分:0 
6楼的解答只考虑了2层数据,有些情况不单但是考虑2曾就可以的,甚至有些情况需要整体分析所有数据.
用穷搜可能可以做出来,不过需要搜2^n/2数据太大了~~~
2006-12-10 11:49
剑风曲
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-11-16
收藏
得分:0 
楼上的谢了,根据你的思路写了一个,答案是对了,但是超时了........
2006-12-10 12:50
剑风曲
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-11-16
收藏
得分:0 
我用了递归~~~可能没完全理解你的意思,~~~最好帮偶把关键代码写一下,全代码就更感谢了,我的代码偶放一下先:
#include<stdio.h>
int p(int x[],int q,int k){
int s,d,t;
if(k==2)return x[q]>x[q+1]?(x[q]-x[q+1]):(x[q+1]-x[q]);
t=p(x,q+1,k-2);
if(x[q+1]>x[k-q-1])s=x[q]-x[q+1]+p(x,q+2,k-2);
else s=x[q]-x[k-q-1]+t;
if(x[q]>x[k-q-2])d=x[k-q-1]-x[q]+t;
else d=x[k-q-1]-x[k-q-2]+p(x,q,k-2);
return d>s?d:s;
}
int main(){
int n;
int a[1000];
int s,j=0;
while(scanf("%d",&n)!=EOF){
if(!n)break;
j++;
for(int i=0;i<n;i++)scanf("%d",&a[i]);
s=p(a,0,n);
printf("In game %d, the greedy strategy might lose by as many as %d points.\n",j,s);
}
}
2006-12-10 13:49
快速回复:Two Ends 求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019864 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved