Advertisement
fireLUFFY

Job-A-Thon 21 Q2

Aug 21st, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. /*
  2. Problem Statement
  3.  
  4. Given an array "arr" of positive integers with length N, your goal is to achieve the maximum possible score by removing elements from either the beginning or the end of the array. The score for removing an element is calculated as:
  5.  
  6.     Score = element * length(arr) + minimum(arr)
  7.  
  8. Here, "arr" is considered before the current operation, and you can choose to remove elements from the start or end of the array.
  9. "element" represents the value of the removed element.
  10.  
  11. TC 1:
  12. N = 3
  13. arr = {2,3,4}
  14. Output: 26
  15. Explanation: A possible way to perform the operations
  16. -> remove 4 from last,
  17. score = 4*length({2,3,4})+min({2,3,4}) = 14
  18. updated arr = {2,3}
  19.  
  20. -> remove 2 from start,
  21. score = 2*length({2,3})+min({2,3}) = 6
  22. updated arr = {3}
  23.  
  24. -> remove last element 3,
  25. score = 3*length({3})+min({3}) = 6
  26.  
  27. -> total score = 14 + 6 + 6 = 26  
  28. */
  29.  
  30.  
  31. class Solution
  32. {
  33.     public:
  34.     int min_in_range(int l,int r,vector<vector<int>>& m,vector<int>& logg)
  35.     {
  36.         int length=(r-l+1),k;
  37.         k=logg[length];
  38.         return min(m[l][k],m[r-(1<<k)+1][k]);
  39.     }
  40.    
  41.     long long func(int l, int r, int n, vector<int>& arr, vector<vector<int>>& m,vector<int>& logg,vector<vector<long long>>& dp){
  42.         if(n==1){
  43.             return (2*arr[l]);
  44.         }
  45.        
  46.         if(dp[l][r]!=-1){
  47.             return dp[l][r];
  48.         }
  49.        
  50.         long long val1=((arr[l]*n + min_in_range(l,r,m, logg)) + func(l+1,r,n-1,arr,m,logg,dp));
  51.         long long val2=((arr[r]*n + min_in_range(l,r,m,logg)) + func(l,r-1,n-1,arr,m,logg,dp));
  52.         return dp[l][r]=max(val1,val2);
  53.     }
  54.    
  55.     long long MaxScore(int N, vector<int>&arr)
  56.     {
  57.         if(N==1){
  58.             return 2*arr[0];
  59.         }
  60.        
  61.         int l=0,r=N-1,len=N;
  62.         vector<vector<int>>m(20005,vector<int>(16));
  63.         vector<int>logg(20005);
  64.         vector<vector<long long>>dp(N+2,vector<long long>(N+2,-1));
  65.        
  66.         logg[1]=0;
  67.         for(int i=2;i<20005;i++){
  68.             logg[i]=logg[i/2]+1;
  69.         }
  70.         for(int i=0;i<N;i++){
  71.             m[i][0]=arr[i];
  72.         }
  73.        
  74.         for(int j=1;j<16;j++){
  75.             for(int i=0;(i+(1<<j)-1)<N;i++){
  76.                 m[i][j]=min(m[i][j-1],m[i+(1<<(j-1))][j-1]);
  77.             }
  78.         }
  79.        
  80.         long long ans=func(l,r,len,arr,m,logg,dp);
  81.         return ans;
  82.     }
  83. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement