ann8497

oil mines Optimized dp Samsung

Sep 8th, 2019
2,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /*
  2. oil mines dp
  3.  
  4. THIS CODE  IS SUBJECT TO COPYRIGHT.
  5.  
  6. TEST CASES
  7.  
  8. 5 10
  9. 2 3 5 7 6 2 1 9 8 5
  10. ans = 4
  11.  
  12. 4 7
  13. 12 3 5 67 54 3 4
  14. ans = 59
  15.  
  16. 8 9
  17. 1 2 3 4 5 6 7 8 9
  18. ans = 6
  19.  
  20. 4 10
  21. 9 18 7 6 3 24 1 5 8 9
  22. ans = 11
  23.  
  24. 1 4
  25. 1 2 3 4
  26. ans = 0
  27.  
  28. 5 4
  29. 1 2 3 4
  30. ans -1
  31.  
  32. 2 15
  33. 1 34 5 7 3 21 90 81 32 4 23 42 84 11 29
  34. ans = 1
  35.  
  36. Time complexity = O(n^2*k^2)
  37. space complexity = O(n*k*2)
  38. */
  39.  
  40. #include<bits/stdc++.h>
  41. using namespace std;
  42.  
  43. int n,c;
  44. int a[100];
  45. int b[100];
  46. int pre[100];
  47. int dp[100][100][2];
  48.  
  49. /* fn to precompute the prefix */
  50. void sum(){
  51.     for(int i=1; i<=n; i++)
  52.        pre[i] = a[i] + pre[i-1];
  53. }
  54.  
  55. int solve(){
  56.    
  57.    for(int j = 1; j<=n; j++){
  58.        for(int k = 1; k<=c && k<=j; k++){
  59.          
  60.           /* Base case */
  61.            if(k == 1){
  62.                dp[j][k][0] =  pre[j];
  63.                dp[j][k][1] =  pre[j];
  64.            }
  65.        
  66.            else{
  67.  
  68.                int temp = INT_MAX;
  69.                for(int l = j; l>k-1; l--){
  70.                    /* magic happens here */
  71.                      
  72.                     int mn = min((pre[j]-pre[l-1]), min(dp[l-1][k-1][0], dp[l-1][k-1][1]));
  73.                     int mx = max((pre[j]-pre[l-1]), max(dp[l-1][k-1][0], dp[l-1][k-1][1]));
  74.                    
  75.                     if(mx-mn < temp){
  76.                         temp = mx - mn;
  77.                         dp[j][k][0] = mn;
  78.                         dp[j][k][1] = mx;
  79.                     }
  80.            
  81.                }
  82.                
  83.                  
  84.            }
  85.          
  86.        }
  87.    }
  88.    //cout<<dp[2][2][0]<<" "<<dp[2][2][1]<<endl;
  89.    return dp[n][c][1] - dp[n][c][0];
  90. }
  91.  
  92.  
  93. int main(){
  94.    
  95.     int t; cin>>t;
  96.     while(t--){
  97.  
  98.         cin>>c;
  99.         cin>>n;
  100.        
  101.         for(int i = 1; i<=n; i++)cin>>b[i];
  102.         /* this is done to iterate for every window size of a circular array */
  103.         for(int i = n+1; i<=2*n; i++)b[i] = b[i-n];
  104.        
  105.         int ans = INT_MAX;
  106.         for(int i = 1; i<=n+1; i++){
  107.                 int j = i+n-1;
  108.                
  109.                 memset(dp,0,sizeof(dp));
  110.                 memset(pre, 0, sizeof(pre));
  111.                
  112.                 /* this is done so that we don't have to map the index
  113.                  due to this everytime the indexing will be from 1-n */
  114.                 int temp  = 1;
  115.                 for(int k = i; k<=j; k++){
  116.                     a[temp] = b[k];
  117.                     temp++;
  118.                 }
  119.                
  120.                  sum();
  121.                  
  122.                  ans = min(ans, solve());
  123.            
  124.         }
  125.        if(c>n)ans = -1;
  126.        cout<<ans<<endl;
  127.     }
  128. }
Add Comment
Please, Sign In to add comment