【区间DP】乘法游戏

 
   
 
乘法游戏
 
   

 

 

 

 

背景 Background

 

 

太原成成中学第2次模拟赛 第四道

 

 

 

 

 

 

 

描述 Description

 

 

乘法游戏是在一行牌上进行的。每一张牌包括了一个正整数。在每一个移动中,玩家拿出一张牌,得分是用它的数字乘以它左边和右边的数,所以不允许拿第1张和最后1张牌。最后一次移动后,这里只剩下两张牌。
  你的目标是使得分的和最小。
  例如,如果数是10 1 50 20 5,依次拿1、20、50,总分是             10*1*50+50*20*5+10*50*5=8000
  而拿50、20、1,总分是1*50*20+1*20*5+10*1*5=1150。

 

 

 

 

 

 

 

输入格式 Input Format

 

 

输入文件的第一行包括牌数(3<=n<=100),第二行包括N个1-100的整数,用空格分开。

 

 

 

 

 

 

 

输出格式 Output Format

 

 

输出文件只有一个数字:最小得分

 

 

 

 

 

 

 

样例输入 Sample Input [复制数据]

 

 

 

 

 

 

 

 

 

样例输出 Sample Output [复制数据]

 

 

 

 

 

 

 

 

 

时间限制 Time Limitation

 

 

各个测试点1s

 

================================

======================

var
  n:longint;
  a:array[1..100]of longint;
  f:array[1..100,1..100]of longint;

procedure init;
begin
  assign(input,'ty1014.in');
  assign(output,'ty1014.out');
  reset(input); rewrite(output);
end;

procedure terminate;
begin
  close(input); close(output);
  halt;
end;

function dp(s,t:longint):longint;
var
  i:longint;
  now:longint;
begin
  if s+1=t then exit(0);
  //if s>t then exit(0);
  if f[s,t]<>maxlongint then exit(f[s,t]);
  dp:=10000000;
  for i:=s+1 to t-1 do
    begin
      if dp>dp(s,i)+dp(i,t)+a[i]*a[s]*a[t] then
        dp:=dp(s,i)+dp(i,t)+a[i]*a[s]*a[t];
    end;
  f[s,t]:=dp;
end;

procedure main;
var
  i,j,k:longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  //fillchar(f,sizeof(f),0);
  for i:=1 to n do
    for j:=1 to n do
      f[i,j]:=maxlongint;
  writeln(dp(1,n));
end;

begin
  init;
  main;
  terminate;
end.


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值