Advertisement
ann8497

Camel (Gurgaon)

Dec 6th, 2020 (edited)
4,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /*
  2. written with simplicity by Ankit Mittal
  3. Note - the code can be further optimized but for n = 8 it wil work fine
  4.  
  5. A man has some camels with him.He has to take these camels to the opposite end.Each camel has some cost to go from one end to other.
  6. ->While going to opposite end , he will take 2 camels along with him, the cost will be the maximum of the cost of both the camels.
  7. ->On reaching opposite end, he will take one camel from opposite end to go back to first end, the cost will be taken as the cost of that camel.
  8. ->At the end, he has to move all the camels to the opposite end.
  9. Find the minimum cost to take camels to opposite end.
  10. Example->
  11. INPUT
  12. 2              //No of test cases
  13. 4              //No of camels in first test case
  14. 1 2 8 9        //Cost of camels
  15. 6
  16. 14 45 73 86 95 98
  17. OUTPUT
  18. 16
  19. 434
  20.  
  21. SOLUTION->
  22. 1) First he will take camel 1 and camel 2 to opposite end , so the cost will be -max(1,2)=2
  23. 2) Next he will come from camel 1 , cost will be- 1.
  24. 3) Now he will take camel 3 and camel 4 to opposite end , cost will be - max(8,9)=9
  25. 4) Now he will come back using camel 2 which was already at opposite end ,cost- 2
  26. 5) Now only camel 1 and camel 2 are left, he will take them both.cost=max(1,2)=2
  27.  
  28. Total cost = 2+1+9+2+2=16
  29. */
  30.  
  31. #include<bits/stdc++.h>
  32. using namespace std;
  33.  
  34. int globalAns = INT_MAX;
  35.  
  36. void solve(int a[], int n, bool vis[], int LtoR, int ans, int visCnt){
  37.  
  38.     if(ans>globalAns) return;                                                       /* optimization */
  39.     if(visCnt == n){                                                                 /*base case // all visited */
  40.        // TO CHECK ALL THE ANSWERS SIMPLY PRINT THE ANS EVERYTIME
  41.        globalAns = min(ans, globalAns);  
  42.     }
  43.    
  44.     if(LtoR == 1){
  45.    
  46.         for(int i = 0; i<n; i++){
  47.             for(int j = i+1; j<n; j++){
  48.              
  49.                 if(vis[i] == false && vis[j] == false){
  50.                     int bada = max(a[i],a[j]);
  51.            
  52.                      /* backtracking logic */
  53.                     vis[i] = true; vis[j] = true;
  54.                     solve(a,n,vis,1-LtoR, ans + bada, visCnt + 2);
  55.                     vis[i] = false; vis[j] = false;
  56.                 }
  57.            
  58.             }
  59.         }
  60.        
  61.     }
  62.     else{
  63.  
  64.         for(int i = 0; i<n; i++){
  65.             if(vis[i] == true){
  66.                /* backtracking logic */
  67.                 vis[i] = false;
  68.                 solve(a,n,vis,1-LtoR, ans + a[i], visCnt - 1);
  69.                 vis[i] = true;
  70.             }
  71.         }
  72.        
  73.     }
  74. }
  75.  
  76.  
  77. int main(){
  78.    
  79.     int t;
  80.     cin>>t;
  81.     // solution by Ankit Mittal
  82.     while(t--){
  83.         int n;
  84.         cin>>n;
  85.         int a[n];
  86.         bool vis[n];
  87.         for(int i = 0; i<n; i++)cin>>a[i];
  88.         for(int i = 0; i<n; i++)vis[i] = false;
  89.        
  90.         solve(a,n,vis,1,0,0);
  91.         cout<<globalAns<<endl;
  92.     }
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement