一道动归问题

                           Card Game
   The multiplication puzzle is played with a row of cards,each containing a single positive integer.The player takes one card out of the row and scores the number of points equal to the number on the card taken and the numbers on the cards on the left and right of it. It is not allowed to take out the first and the last card in the row.
The goal is to take cards in such order as to minimize the total number of socred points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1,then 20,then50,scoring
10*1*50+50*20*5+10*50=8000
if he would take the cards in the opposite order,i.e.50,then 20, then 1,scoring
1* 50*20+1*20*5+10*1*5=1150.

Input:
The first line contains the number of cards N(3<=N<=1000)
The seconde line contains N integers in the range from 1 to 100, separated by spaces.
Ouput:
Ouput a single integer--the minimal score.
Examples:
4
5 4 1 3
35
-----------
3
1 2 3
6
 

#include  < iostream >
#include 
< cstring >
using   namespace  std;
const   int  total = 1000 ;
int  f[total][total];
int  value[total];
int  n;
void  Init()
{
  memset(f,
0,sizeof(f));
  memset(value,
0,sizeof(value));     
}

void  Input()
{
    Init();
    
int i;
    cin
>>n;
    
for(i=0;i<n;i++)
     cin
>>value[i];    
}

void  Solve()
{
    
int i,j,k,m,min,tmp;
     
for(i=0;i<n-1;i++)
       f[i][i
+1]=0;
     
for(k=2;k<n;k++)
      
for(i=0;i+k<n;i++)
       
{
         j
=i+k;
         min
=f[i+1][j]+value[i]*value[i+1]*value[j];
           
for(m=i+2;m<j;m++)
           
{
                tmp
=f[i][m]+f[m][j]+value[i]*value[m]*value[j];
                
if(tmp<min) min=tmp;
           }
 
           f[i][j]
=min;     
       }
 
    cout
<<f[0][n-1]<<endl;
}

int  main()
{
    
    Input();
    Solve();
    system(
"pause");
return 0;
}

O(n^3)的算法,n=1000时估计抗不住。。。。。好的算法没想到,好长时间没比赛思维都退化了。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
买书问题 dp实现 题目:买书 有一书店引进了一套书,共有3卷,每卷书定价是60元,书店为了搞促销,推出一个活动,活动如下: 如果单独购买其中一卷,那么可以打9.5折。 如果同时购买两卷不同的,那么可以打9折。 如果同时购买三卷不同的,那么可以打8.5折。 如果小明希望购买第1卷x本,第2卷y本,第3卷z本,那么至少需要多少钱呢?(x、y、z为三个已知整数)。 1、过程为一次一次的购买,每一次购买也许只买一本(这有三种方案),或者买两本(这也有三种方案), 或者三本一起买(这有一种方案),最后直到买完所有需要的书。 2、最后一步我必然会在7种购买方案中选择一种,因此我要在7种购买方案中选择一个最佳情况。 3、子问题是,我选择了某个方案后,如何使得购买剩余的书能用最少的钱?并且这个选择不会使得剩余的书为负数 。母问题和子问题都是给定三卷书的购买量,求最少需要用的钱,所以有"子问题重叠",问题中三个购买量设置为参数, 分别为i、j、k。 4、的确符合。 5、边界是一次购买就可以买完所有的书,处理方式请读者自己考虑。 6、每次选择最多有7种方案,并且不会同时实施其中多种,因此方案的选择互不影响,所以有"子问题独立"。 7、我可以用minMoney[i][j][k]来保存购买第1卷i本,第2卷j本,第3卷k本时所需的最少金钱。 8、共有x * y * z个问题,每个问题面对7种选择,时间为:O( x * y * z * 7) = O( x * y* z )。 9、用函数MinMoney(i,j,k)来表示购买第1卷i本,第2卷j本,第3卷k本时所需的最少金钱,那么有: MinMoney(i,j,k)=min(s1,s2,s3,s4,s5,s6,s7),其中s1,s2,s3,s4,s5,s6,s7分别为对应的7种方案使用的最少金钱: s1 = 60 * 0.95 + MinMoney(i-1,j,k) s2 = 60 * 0.95 + MinMoney(i,j-1,k) s3 = 60 * 0.95 + MinMoney(i,j,k-1) s4 = (60 + 60) * 0.9 + MinMoney(i-1,j-1,k) s5 = (60 + 60) * 0.9 + MinMoney(i-1,j,k-1) s6 = (60 + 60) * 0.9 + MinMoney(i-1,j,k-1) s7 = (60 + 60 + 60) * 0.85 + MinMoney(i-1,j-1,k-1)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值