Advertisement
Josif_tepe

Untitled

Apr 24th, 2023
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <map>
  7. #include <stack>
  8. #include <set>
  9. using namespace std;
  10. const int maxn = 1005;
  11. typedef long long ll;
  12. int dp[maxn][maxn];
  13. ll pref_sum[maxn];
  14. ll sum(int L, int R) {
  15.     return pref_sum[R] - pref_sum[L - 1];
  16. }
  17. ll bin_search(int i, int j, ll s) {
  18.     int L = i, R = j;
  19.     while(L <= R) {
  20.         int mid = (L + R) / 2;
  21.         ll x = sum(i, mid);
  22.         if(x == s and (mid == i or sum(i, mid - 1) != s)) {
  23.             return mid;
  24.         }
  25.         else if(x >= s) {
  26.             R = mid - 1;
  27.         }
  28.         else {
  29.             L = mid + 1;
  30.         }
  31.     }
  32.     return -1;
  33. }
  34. int rec(int i, int j) {
  35.     ll s = sum(i, j);
  36.     if(i < j and s % 2 == 0) {
  37.         int idx = bin_search(i, j, s / 2);
  38.         if(idx != -1) {
  39.             return max(rec(i, idx), rec(idx + 1, j)) + 1;
  40.          }
  41.     }
  42.     return 0;
  43. }
  44. int main() {
  45.     ios_base::sync_with_stdio(false);
  46.     int t;
  47.     cin >> t;
  48.     while(t--) {
  49.         int n;
  50.         cin >> n;
  51.         vector<int> v(n);
  52.         ll s = 0;
  53.         for(int i =0 ; i < n; i++) {
  54.             cin >> v[i];
  55.             s += v[i];
  56.             pref_sum[i] = s;
  57.         }
  58.         cout << rec(0, n - 1) << endl;
  59.     }
  60.     return 0;
  61. }
  62. /*
  63.  5 9 7
  64. ...##....
  65. ..#.##..#
  66. ..#....##
  67. .##...#..
  68. ....#....
  69. WS?EE??
  70.  
  71.  
  72.  **/
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement