Advertisement
RaFiN_

knuth optimization in D&C

May 12th, 2020
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. LL call(int group, int pos)
  2. {
  3.     if(pos == 0)
  4.         return dp[group][pos] = 0;
  5.     if(group == 0)
  6.         return dp[group][pos] = inf;
  7.  
  8.     if(dp[group][pos] != -1)
  9.         return dp[group][pos];
  10.  
  11.     int L = 1, R = pos;
  12.     if(pos-1 > 0)
  13.     {
  14.         call(group, pos-1);
  15.         L = max(L, path[group][pos-1]);
  16.     }
  17.     if(group+1 <= k)
  18.     {
  19.         call(group+1, pos);
  20.         R = min(R, path[group+1][pos]);
  21.     }
  22.     LL ret = inf;
  23.     for(int i = L; i<=R; i++)
  24.     {
  25.         LL cur = call(group-1, i-1) + (cum[pos] - cum[i-1])*(pos-i+1);
  26.         if(cur < ret)
  27.         {
  28.             ret = cur;
  29.             path[group][pos] = i;
  30.         }
  31.     }
  32.     return dp[group][pos] = ret;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement