matheus_monteiro

Mububa

May 6th, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. const int OO = 0x3f3f3f3f3f3f3f3f;
  5.  
  6. int n, arr[3005], pf[3005];
  7. int dp[3005][3005];
  8.  
  9. int32_t main()
  10. {
  11.     ios_base::sync_with_stdio(false);
  12.     cin.tie(nullptr);
  13.  
  14.     cin >> n;
  15.     for(int i = 1; i <= n; i++)
  16.         cin >> arr[i], pf[i] = pf[i - 1] + arr[i];
  17.  
  18.     memset(dp, 63, sizeof(dp));
  19.    
  20.     for(int i = 1; i <= n; i++)
  21.         dp[i][1] = pf[i];
  22.  
  23.     for(int r = 2; r <= n; r++) {
  24.  
  25.         for(int i = 1; i <= n; i++) {
  26.    
  27.             int b = 1, e = i - 1;
  28.  
  29.             while(b <= e) {
  30.                 int mid = (b + e) / 2;
  31.                
  32.                 if(dp[mid][r - 1] >= OO)
  33.                     b = mid + 1;
  34.                 else if(dp[mid][r - 1] <= pf[i] - pf[mid]) {
  35.                     dp[i][r] = pf[i] - pf[mid];
  36.                     b = mid + 1;
  37.                 } else
  38.                     e = mid - 1;
  39.             }
  40.  
  41.             /*
  42.             for(int j = 1; j < i; j++) {
  43.                 if(pf[i] - pf[j] >= dp[j][r - 1])
  44.                     dp[i][r] = min(dp[i][r], pf[i] - pf[j]);
  45.             }
  46.             */
  47.         }
  48.    
  49.     }
  50.  
  51.     int ans = 0;
  52.  
  53.     for(int i = 1; i <= n; i++)
  54.         if(dp[n][i] < OO)
  55.             ans = i;
  56.  
  57.     cout << ans << '\n';
  58.    
  59.  
  60.     return 0;
  61. }
  62.  
  63. /*
  64.    Before submit:
  65.       Check the corners cases
  66.       Check solution restrictions
  67.  
  68.    For implementation solutions:
  69.       Check the flow of the variables
  70.  
  71.    For intervals problems:
  72.       Think about two pointers
  73.  
  74.    For complete search:
  75.       Think about cuts
  76.  
  77.    If you get WA:
  78.       Reread your code looking for stupid typos
  79.       Try various manual testcases
  80.       Recheck the correctness of your algorithm
  81.       Reread the statement
  82.       Write a straightforward solution, create lots of testcases by yourself, and compare both solutions
  83.       Change the coder (if you're with a team)
  84.       Give up. You may have other tasks to solve.
  85. */
  86.  
Add Comment
Please, Sign In to add comment