Pascal classic algorithm explanation - stone merging problem

From , 5 Years ago, written in Delphi (Object Pascal), viewed 226 times.
URL https://pastebin.vip/view/12fb63ba
  1.  
  2. var
  3.  n:integer;
  4.  a:array[1..100] of longint;
  5.  s:array[1..100,1..100] of longint;
  6.  t:array[0..100,0..100] of longint;
  7.  i,j,k,temp,max,min:longint;
  8. begin
  9.   assign(input,'shizi.in');
  10.   reset(input);
  11.   readln(n);
  12.   fillchar(t,sizeof(t),0);      {计算和数组}
  13.   for i:=1 to n do
  14.     read(a[i]);
  15.   for i:=1 to n do
  16.     for j:=1 to n do
  17.       for k:=i to i+j-1 do
  18.         begin
  19.           if k>n then temp:=k mod n else temp:=k;
  20.           t[i,j]:=t[i,j]+a[temp];
  21.         end;
  22. {动态规划求最大得分}
  23.   fillchar(s,sizeof(s),0);
  24.   for j:=2 to n do
  25.     for i:=1 to n do
  26.       for k:=1 to j-1 do
  27.         begin
  28.           if i+k>n then temp:=(i+k) mod n else temp:=i+k;  {处理环形问题}
  29.           max:=s[i,k]+s[temp,j-k]+t[i,j];
  30.           if s[i,j]<max then s[i,j]:=max;
  31.         end;
  32.   max:=0;        {在最后的阶段状态中找最大得分}
  33.   for i:=1 to n do
  34.     if max<s[i,n] then max:=s[i,n];
  35.  
  36. {动态规划求最小得分}
  37.   fillchar(s,sizeof(s),0);
  38.   for j:=2 to n do
  39.     for i:=1 to n do
  40.       begin
  41.         min:=maxlongint;
  42.         for k:=1 to j-1 do
  43.           begin
  44.             if i+k>n then temp:=(i+k) mod n else temp:=i+k;  {处理环形问题}
  45.             s[i,j]:=s[i,k]+s[temp,j-k]+t[i,j];
  46.             if min>s[i,j] then min:=s[i,j];
  47.           end;
  48.         s[i,j]:=min;
  49.       end;
  50.   min:=maxlongint;  {在最后的阶段状态中找最小得分}
  51.   for i:=1 to n do
  52.     if min>s[i,n] then min:=s[i,n];
  53.  
  54.   writeln(max);
  55.   writeln(min);
  56. end.
  57.  
  58. //delphi/7197

Reply to "Pascal classic algorithm explanation - stone merging problem"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website