fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool visit[100020];
  5. int main(){
  6.  
  7. #ifdef PARTHO
  8. freopen("C:\\Users\\parth\\Desktop\\input.txt","r",stdin);
  9. freopen("C:\\Users\\parth\\Desktop\\output.txt","w",stdout);
  10. int start_time = clock();
  11. #endif
  12.  
  13. queue<int> enter;
  14. int n;
  15. cin >> n;
  16. int value;
  17. for( int i= 0;i<n;i++)
  18. {
  19. cin >> value;
  20. enter.push(value);
  21. }
  22.  
  23. int fine = 0;
  24. for(int i = 0;!enter.empty();i++)
  25. {
  26. cin >> value;
  27. int top = enter.front();
  28. while(visit[top])
  29. {
  30. enter.pop();
  31. top = enter.front();
  32. }
  33.  
  34. if(value==top)
  35. {
  36. visit[value] = 1;
  37. enter.pop();
  38. }
  39. else
  40. {
  41. if(!visit[value])
  42. fine++;
  43.  
  44. visit[value] = 1;
  45.  
  46. }
  47.  
  48.  
  49. }
  50. cout << fine << endl;
  51.  
  52.  
  53. #ifdef PARTHO
  54. int end_time = clock();
  55. printf("Time = %f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
  56. #endif
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 4504KB
stdin
5
3 5 2 1 4
4 3 2 5 1
stdout
2