ann8497

fishery samsung

Sep 7th, 2019
2,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. /*
  2. inputs
  3. 5
  4. 10
  5. 4 5
  6. 6 2
  7. 10 2
  8. 10
  9. 8 5
  10. 9 1
  11. 10 2
  12. 24
  13. 15 3
  14. 20 4
  15. 23 7
  16. 39
  17. 17 8
  18. 30 5
  19. 31 9
  20. 60
  21. 57 12
  22. 31 19
  23. 38 16
  24. outputs
  25. 18
  26. 25
  27. 57
  28. 86
  29. 339
  30. */
  31.  
  32.  
  33. #include<bits/stdc++.h>
  34. using namespace std;
  35.  
  36. int pos[4];    // pos represent the position of the slots
  37. int val[4];  // values corresponds to the number of fishermens
  38. int a[1000];
  39.  
  40. int n, ans = INT_MAX;
  41. /* finding the nearest right empty position*/
  42. int posRight(int index){
  43.     for(int i = index; i<=n; i++)
  44.     if(a[i] == 0)return i;
  45.     return INT_MAX;
  46. }
  47.  
  48. /*finding the nearest left most empty position*/
  49. int posLeft(int index){
  50.     for(int i = index -1; i>0; i--)
  51.     if(a[i] == 0)return i;
  52.     return INT_MAX;
  53. }
  54.  
  55.  
  56. void solve(int x, int y, int z, int currPos, int cost){
  57.    
  58.   if(cost > ans)return;
  59.  
  60.   if(val[currPos] == 0){
  61.       if(currPos == x)currPos = y;
  62.       else if(currPos == y)currPos = z;
  63.       else{
  64.           ans = min(ans,cost);
  65.           return;
  66.       }
  67.   }
  68.  
  69.    int l  = posLeft(pos[currPos]);
  70.    int ll = abs(pos[currPos] - l + 1);
  71.    int r  = posRight(pos[currPos]);
  72.    int rr = abs(r - pos[currPos] + 1);
  73.  
  74.    
  75.    int distance = ll>rr ? r : l;
  76.    int tempx = min(ll,rr);
  77.  
  78.    /* in case of last position if there is equal distance of left and right then check for
  79.       both the cases any of the following will give the optimal results*/
  80.  
  81.        if(val[currPos] == 1 && ll == rr){
  82.            
  83.            a[l] = 1;
  84.            val[currPos]--;
  85.            solve(x, y, z, currPos, cost + ll);
  86.            a[l] = 0;
  87.            val[currPos]++;
  88.            
  89.            a[r] = 1;
  90.            val[currPos]--;
  91.            solve(x, y, z, currPos, cost + rr);
  92.            a[r] = 0;
  93.            val[currPos]++;
  94.        }
  95.        
  96.    /* optimization is done here we are choosing the pos which is near*/
  97.     else{
  98.        a[distance] = 1;
  99.        val[currPos]--;
  100.        solve(x,y,z,currPos, cost + tempx);
  101.        a[distance] = 0;
  102.        val[currPos]++;
  103.     }
  104. }
  105.  
  106. void init(){
  107.      for(int i = 0; i<1000; i++)a[i] = 0;
  108. }
  109.  
  110. int main(){
  111.    
  112.     int t; cin>>t;
  113.     for(int i = 0; i<t; t--){
  114.        
  115.         cin>>n;
  116.        
  117.         for(int i = 1; i<=3; i++)
  118.         cin>>pos[i]>>val[i];
  119.        
  120.         ans = INT_MAX;
  121.         init();
  122.        
  123.         solve(1,2,3,1,0);init();
  124.         solve(1,3,2,1,0);init();
  125.         solve(2,3,1,2,0);init();
  126.         solve(2,1,3,2,0);init();
  127.         solve(3,1,2,3,0);init();
  128.         solve(3,2,1,3,0);init();
  129.      
  130.       cout<<ans<<endl;
  131.        
  132.     }
  133.    
  134.     return 0;
  135. }
Add Comment
Please, Sign In to add comment