1. 程式人生 > >【區間DP】乘法遊戲

【區間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.