Advertisement
Kali_prasad

cabinet partitioning

Mar 26th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef vector<int> vi;
  11. typedef vector<string> vs;
  12. typedef vector<ll> vll;
  13. typedef vector<vector<int>> vvi;
  14. typedef vector<vector<string>> vvs;
  15. typedef vector<vector<ll>> vvll;
  16.  
  17. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  18. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  19. #define sz(x) (int)(x).size()
  20. #define mp make_pair
  21. #define pb push_back
  22. #define f first
  23. #define s second
  24. #define ins insert
  25.  
  26. const int MOD = 1000000007;
  27. //type functions here
  28. int ans;
  29. void ptns(vi &v,int prt,int idx,int max_sum)
  30. {
  31.     int sum=0;
  32.     if(idx>=sz(v))
  33.         return;
  34.     if(prt==1)
  35.     {
  36.         while(idx!=sz(v))
  37.         {
  38.         sum+=v[idx];
  39.         idx++;
  40.         }
  41.         max_sum=max(max_sum,sum);
  42.         ans=min(ans,max_sum);
  43.         return;
  44.     }
  45.     FOR(i,idx,sz(v)-prt)//important
  46.     {
  47.         sum+=v[i];
  48.         max_sum=max(sum,max_sum);
  49.         ptns(v,prt-1,i+1,max_sum);
  50.     }
  51. }
  52.  
  53. int main() {
  54.    
  55.     int tc;
  56.     cin>>tc;
  57.    
  58.     FOR(i,1,tc)
  59.     {
  60.         ans=INT_MAX;
  61.         int size,prt;
  62.         vi v;
  63.         cin>>size>>prt;
  64.         FOR(j,1,size)
  65.         {
  66.            int temp;
  67.            cin>>temp;
  68.            v.pb(temp);  
  69.         }
  70.         ptns(v,prt,0,INT_MIN);
  71.         cout<<ans<<endl;
  72.     }
  73.     return 0;
  74. }
  75.  
  76.  
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement