fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN=1e5;
  5. int t[maxN];
  6. vector<int> stu[maxN+1];
  7.  
  8. int main(){
  9. ios::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11. int N, x;
  12. cin>>N;
  13. for(int i=0; i<N; i++)
  14. cin>>t[i];
  15.  
  16. for(int i=0; i<N; i++){
  17. cin>>x;
  18. stu[x].push_back(i);
  19. }
  20. for(int i=0; i<N; i++){
  21. int n=t[i];
  22. int s=stu[n].size();
  23. if(stu[n].size()==0){
  24. cout<<"-1 ";
  25. continue;
  26. }
  27. int best=abs(stu[n][0]-i);
  28. for(int j=1; j<s; j++){
  29. best=min(abs(stu[n][j]-i), best);
  30. }
  31. cout<<best<<' ';
  32. }
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5900KB
stdin
6
1 3 5 2 4 2
1 9 2 4 5 6
stdout
0 -1 2 1 1 3