求算法

chenbing3313 2012-06-07 02:44:46
题目如下:
Suppose you play the following game with your opponent:
There is a sequence of n coins with values v[1], v[2], …, v[n] in a queue which are known in advance. Now each player takes alternate turn to pick a coin. The rule is you can only take the coin from the head or from the tail of the queue each time.
(a) Please design a dynamic program algorithm such that the total value of the coins you pick is the largest. We assume the opponent is very stupid and always makes an incorrect decision. We assume you make the first pick. Pseudo code is not required but you need to show the inductive formula and initial conditions for the program. What is the complexity of your algorithm?
(b) Apply your algorithm to the following sequence: 7, 5, 2, 3, 1, 6.

题干:
A、B二人在一序列中取数。
取数规则:
1、为只能在序列两端取数据。
2、A先取。
3、A、B交替取数据。
3、B只可以去他当前可获取的数据中最小的一个。
目的:给一个算法,使A取得的数据总和最大。
...全文
126 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dpdp_2012 2012-11-01
  • 打赏
  • 举报
回复
细节不知道有没有错,思路肯定没问题,动态规划的思想

#include <stdio.h>

int main()
{
int n,a[101],p[101][101];
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=0;i<=n;i++)
p[i][0]=0;
for(int i=1;i<=n;i++)
p[i][1]=a[i];
for(int i=2;i<=n;i++)
{
for(int j=1;j+i-1<=n;j++)
{
int k1,k2;
if(a[j+1]<a[j+i-1])
k1=p[j+2][i-2]+a[j];
else
k1=p[j+1][i-2]+a[j];
if(a[j]<a[j+i-2])
k2=p[j+1][i-2]+a[j+i-1];
else
k2=p[j][i-2]+a[j+i-1];
p[j][i]=k1>k2?k1:k2;
}
}
printf("%d\n",p[1][n]);
}
}
chenbing3313 2012-11-01
  • 打赏
  • 举报
回复
没有答案啊

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧