fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int n;
  7. cin>>n;
  8.  
  9. vector<int>target(n),cur(n);
  10.  
  11. for(int i=0;i<n;i++){
  12. cin>>target[i];
  13. }
  14.  
  15. for(int i=0;i<n;i++){
  16. cin>>cur[i];
  17. }
  18.  
  19. unordered_map<int,int>pos;
  20. for(int i=0;i<n;i++){
  21. pos[cur[i]]=i;
  22. }
  23.  
  24. int longest=0;
  25. int i=0;
  26. while(i<n){
  27. int count=1;
  28. int j=i;
  29.  
  30. while(j<n-1){
  31. if(pos[target[j]]<pos[target[j+1]]){
  32. count++;
  33. }else{
  34. break;
  35. }
  36. j++;
  37. }
  38. longest=max(longest,count);
  39. i=j+1;
  40. }
  41. cout<<n-longest<<endl;
  42. return 0;
  43.  
  44. }
Success #stdin #stdout 0s 5304KB
stdin
6
4 2 3 1 5 6
3 1 4 6 5 2 
stdout
3