Advertisement
_no0B

Untitled

Dec 13th, 2021
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. /// problem: https://acm.timus.ru/problem.aspx?space=1&num=1152
  2.  
  3. int Solve(int mask, int n, int sum)  /// 0 => destroyed
  4. {
  5.     if(mask == 0) return 0;
  6.     int ans = 0;
  7.     for(int i = 0 ; i < n ; i++)
  8.     {
  9.         int newMask = mask, newSum = sum;
  10.         for(int j = 0 ; j < 3 ; j++)
  11.         {
  12.             int k = i + j;
  13.             if(k >= n) k -= n;
  14.             if( ( newMask & (1<<k) ) > 0 )
  15.             {
  16.                 newMask ^= 1<<k;
  17.                 newSum -= arr[k];
  18.             }
  19.         }
  20.         if(newMask != mask)
  21.         {
  22.  
  23.             ans = min(ans, Solve(newMask, n, newSum) + newSum);
  24.         }
  25.     }
  26. } /// O(2^n * n)
  27.  
  28.  
  29. int main()
  30. {
  31.     cout<<Solve((1<<n) - 1, n, sum);
  32.  
  33.     dpp[0] = 0;
  34.  
  35.     for(int mask = 1 ; mask < (1<<n) ; mask++) /// all possible state
  36.     {
  37.         int sum = preCal[mask];
  38.         int ans = 0;
  39.         for(int i = 0 ; i < n ; i++)
  40.         {
  41.             int newMask = mask, newSum = sum;
  42.             for(int j = 0 ; j < 3 ; j++)
  43.             {
  44.                 int k = i + j;
  45.                 if(k >= n) k -= n;
  46.                 if( ( newMask & (1<<k) ) > 0 )
  47.                 {
  48.                     newMask ^= 1<<k;
  49.                     newSum -= arr[k];
  50.                 }
  51.             }
  52.             if(newMask != mask)
  53.             {
  54.  
  55. //                ans = min(ans, Solve(newMask, n, newSum) + newSum);
  56.                 ans = min(ans, dpp[newMask] + newSum);
  57.             }
  58.         }
  59.     }
  60.  
  61.     cout<<dpp[(1<<n) - 1];
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement