fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n; cin >> n;
  12. vector<int> sorted((n)), ans(n);
  13. deque<int> v(n);
  14. for (int i = 0; i < n; i++) {
  15. cin >> v[i];
  16. sorted [i] = v[i];
  17. }
  18. sort(sorted.begin(), sorted.end());
  19. int ctr = 0;
  20. for (int i = n-1; i >=0; i--) {
  21. int j = 0;
  22. while (sorted[i] != v[j] && j < n - ctr - 1) {
  23. j++;
  24. }
  25. if (sorted[i] == v[j]) {
  26. ctr++;
  27. int c = 0;
  28. while (v.back()!=sorted[i]) {
  29. int x = v.front ();
  30. v.pop_front ();
  31. v.push_back(x);
  32. c++;
  33. }
  34. v.pop_back();
  35. ans[i] = c;
  36. }
  37. }
  38. for (int i = 0; i < n; i++) {
  39. cout << ans[i] << " ";
  40. }
  41. cout << endl;
  42. }
  43.  
  44. }
  45.  
Success #stdin #stdout 0.01s 5264KB
stdin
3
6
3 2 5 6 1 4
3
3 1 2
8
5 8 1 3 2 6 4 7
stdout
0 1 1 2 0 4 
0 0 1 
0 1 2 0 2 5 6 2