Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const int OO = 0x3f3f3f3f3f3f3f3f;
- int n, arr[3005], pf[3005];
- int dp[3005][3005];
- int32_t main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin >> n;
- for(int i = 1; i <= n; i++)
- cin >> arr[i], pf[i] = pf[i - 1] + arr[i];
- memset(dp, 63, sizeof(dp));
- for(int i = 1; i <= n; i++)
- dp[i][1] = pf[i];
- for(int r = 2; r <= n; r++) {
- for(int i = 1; i <= n; i++) {
- int b = 1, e = i - 1;
- while(b <= e) {
- int mid = (b + e) / 2;
- if(dp[mid][r - 1] >= OO)
- b = mid + 1;
- else if(dp[mid][r - 1] <= pf[i] - pf[mid]) {
- dp[i][r] = pf[i] - pf[mid];
- b = mid + 1;
- } else
- e = mid - 1;
- }
- /*
- for(int j = 1; j < i; j++) {
- if(pf[i] - pf[j] >= dp[j][r - 1])
- dp[i][r] = min(dp[i][r], pf[i] - pf[j]);
- }
- */
- }
- }
- int ans = 0;
- for(int i = 1; i <= n; i++)
- if(dp[n][i] < OO)
- ans = i;
- cout << ans << '\n';
- return 0;
- }
- /*
- Before submit:
- Check the corners cases
- Check solution restrictions
- For implementation solutions:
- Check the flow of the variables
- For intervals problems:
- Think about two pointers
- For complete search:
- Think about cuts
- If you get WA:
- Reread your code looking for stupid typos
- Try various manual testcases
- Recheck the correctness of your algorithm
- Reread the statement
- Write a straightforward solution, create lots of testcases by yourself, and compare both solutions
- Change the coder (if you're with a team)
- Give up. You may have other tasks to solve.
- */
Add Comment
Please, Sign In to add comment