Advertisement
RaFiN_

seereja and two sequences

Aug 24th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1.  
  2.  
  3. /* seereja and two sequences */
  4.  
  5. /* In thgis problem we will use dynamic programming: dpi, j — minimal pozition of deleted element in second array, such that we have made first operation j times and have deleted not more then i elements from first array.
  6.  
  7. Lets decided how to calculate transfers. Standing in pozition dpi, j we can change nothing and go to pozition dpi + 1, j, by other words make transfer dpi + 1, j:  = min(dpi + 1, j, dpi, j). What happens when we make first operation with fixed prefix(by i-th element) in first array? We should find element in second array with number greater dpi, j and value equal to ai, lets its pozition is t, so we need to make transfer dpi + 1, j + 1:  = min(dpi + 1, j + 1, t).
  8.  
  9. How to find required element quickly: lets just do vector of pozition in second array for all different elements that contains in second array. Then we can simply use binary search.  */
  10.  
  11. int arr[MAX],brr[MAX];vi v[MAX];int id[MAX];int I[MAX],pos[MAX];
  12.  
  13. int dp[350][MAX];
  14.  
  15.  
  16. int main()
  17. {
  18.     booster()
  19.     //read("input.txt");
  20.  
  21.     int n,m,s,e;
  22.     cin>>n>>m>>s>>e;
  23.    
  24.     for(int i = 1;i<=n;i++)cin>>arr[i];
  25.     for(int i = 1;i<=m;i++){
  26.         cin>>brr[i];
  27.         v[brr[i]].pb(i);    
  28.     }
  29.  
  30.     int maxn = s/e;
  31.     int ans = 0;
  32.    
  33.     dp[0][0] = 0;
  34.     for(int i = 1;i<=maxn;i++){
  35.         dp[i][0] = inf / 10;
  36.         for(int j = 1;j<=n;j++){
  37.             auto x = upper_bound(all(v[arr[j]]),dp[i-1][j-1]) ;
  38.             if(x==v[arr[j]].end()){
  39.                 dp[i][j] = inf / 10;
  40.             }
  41.             else dp[i][j] = *x;
  42.             dp[i][j] = min(dp[i][j-1],dp[i][j]);
  43.         }
  44.     }
  45.     for(int i = 1;i<=n;i++){
  46.         for(int j = maxn;j>=1;j--){
  47.             if(i + dp[j][i] + (j*e)<=s)ans = max(ans,j);
  48.         }
  49.     }
  50.     cout<<ans;
  51.    
  52.  
  53.  
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement