ann8497

MaximizePoleHeightForBanner Samsung

Aug 20th, 2019
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. /*
  2.  
  3. /*
  4. You have to place an electronic banner of a company as high as it can be, so that whole the city can view the banner standing on top of TWO PILLERS.
  5. The height of two pillers are to be chosen from given array.. say [1, 2, 3, 4, 6]. We have to maximise the height of the two pillars standing side by side, so that the pillars are of EQUAL HEIGHT and banner can be placed on top of it.
  6. In the above array, (1, 2, 3, 4, 6) we can choose pillars like this, say two pillars as p1 and p2..
  7. Then pillars can be,
  8. p1 = 3 unit… Choosing element (3) from array,
  9. Similarly p2 = 3 choosing (2 + 1) from array.
  10. Since, two pillars are equal, we can put board on it…
  11. But we have two maximise the height of the pillars,
  12. And if we check for other heights, we can see p1 = 6 p2 = 4 + 2 which is greater than 3 ( the previous height)..
  13. We have to see if we can further maximize the height… Yes it can be 8.
  14. I.e. p1 = 6 + 2 = 8. p2 = 4 + 3 + 1 = 8.
  15. Both pillars are equal and banner can be placed… And since this is the maximum height attainable for two pillars, we print the answer as 8. In case, there is no combination possible, print 0 (zero).
  16.  
  17. INPUT :
  18. 1
  19. 5
  20. 1 2 3 4 6
  21. First line is  T  number of test cases to be followed.
  22. Second line of input is number of different pillars.
  23. Third line of input is  different available heights of pillars.
  24. Note : heights of given pillars can be same .. I.e. array can have same elements repeated.
  25.  
  26. Output.
  27. 8
  28. */
  29.  
  30. // both are working good
  31.  
  32. #include<iostream>
  33. using namespace std;
  34.  
  35. #define temp 1000
  36.  
  37. int dp[51][2002];
  38. bool visited[51][2002];
  39. int a[50],n;
  40.  
  41. int solve(int index, int sum){
  42.    
  43.     if(visited[index][sum + temp])
  44.       return dp[index][sum + temp];
  45.      
  46.       visited[index][sum + temp] = true;
  47.      
  48.      int &ans = dp[index][sum + temp];
  49.       ans = -temp;
  50.      
  51.       if(index == n){
  52.           if(sum == 0)ans = 0;
  53.       }
  54.       /* remember there is no for last time you put a for loop */
  55.       else{
  56.           int x = max(ans , a[index] + solve(index + 1, sum + a[index]));
  57.           int y = max(ans , 0        + solve(index + 1, sum - a[index]));
  58.           int z = max(ans , 0        + solve(index + 1, sum));
  59.           ans = max(x,max(y,z));
  60.       }
  61.      
  62.       return ans;
  63. }
  64.  
  65. int main(){
  66.    
  67.     cin>>n;
  68.     for(int i = 0; i<n; i++)cin>>a[i];
  69.    
  70.     if(solve(0,0) <= 0)
  71.       cout<<0<<endl;
  72.     else
  73.       cout<<solve(0,0)<<endl;
  74.    
  75.     return 0;
  76. }
Add Comment
Please, Sign In to add comment