lelouche29

Balloon_busrt_Samsung

Sep 11th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /*
  2. There are n balloons and n bullets and each balloon is assigned with a particular number (point). Whenever a particular balloon is shot the no of points increases by 1.the multiplication of point assigned to balloon on left and that of right side.
  3.  
  4. 2.point assigned to left if no right exists
  5.  
  6. 3.point assigned to right if no left exists.
  7.  
  8. 4.the point assigned to itself if no other balloon exists.
  9.  
  10. You have to output the maximum no of points possible.
  11.  
  12. Input
  13.  
  14. 1 2 3 4
  15.  
  16. Output
  17.  
  18. 20
  19. */
  20.  
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int n;
  25. int dp[100][100];
  26. int a[100];
  27.  
  28. int solve(){
  29.     for(int i=1; i<=n; i++){
  30.         for(int left=1; left <= n-i+1; left++){
  31.             int right=left + i -1;
  32.             for(int k=left; k<=right; k++){
  33.  
  34.                 int temp = dp[left][k-1] + dp[k+1][right];
  35.                 if(left == 1 && right == n) temp += a[k];
  36.                 else temp += a[left-1]*a[right+1];
  37.  
  38.                 dp[left][right]=max(dp[left][right],temp);
  39.  
  40.             }
  41.         }
  42.     }
  43.     return dp[1][n];
  44. }
  45.  
  46. int main() {
  47.     int t;
  48.     cin>>t;
  49.     while(t--){
  50.         cin>>n;
  51.  
  52.         a[0]=1;
  53.         for(int i=1; i<=n; i++)
  54.             cin>>a[i];
  55.         a[n+1]=1;
  56.        
  57.         dp[100][100]={};
  58.         int ans= solve();
  59.         cout<<ans<<endl;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment