fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define vi vector<int>
  6. #define v2i vector<vi>
  7. #define ii pair<int, int>
  8. #define F first
  9. #define S second
  10. #define all(X) (X).begin(), (X).end()
  11. #define quicksilver ios_base::sync_with_stdio(0), cin.tie(0)
  12.  
  13. int tc = 1;
  14.  
  15. signed main()
  16. {
  17. quicksilver;
  18. cin >> tc;
  19. vi sqrs;
  20. for (int i = 0; i <= 2e5; i++)
  21. sqrs.push_back(i * i);
  22.  
  23. while (tc--)
  24. {
  25. int n;
  26. cin >> n;
  27. vi a(n), b(n);
  28. for (int i = 0; i < n; i++)
  29. a[i] = i;
  30.  
  31. b = a;
  32.  
  33. int idx = n - 1;
  34. while (idx >= 0)
  35. {
  36.  
  37. int curr_num = a[idx];
  38. int actual_num = *lower_bound(all(sqrs), a[idx]);
  39.  
  40. int dist = actual_num - curr_num;
  41. reverse(b.begin() + dist, b.begin() + curr_num + 1);
  42.  
  43. idx = dist - 1;
  44. }
  45.  
  46. for (auto x : b)
  47. cout << x << ' ';
  48. cout << '\n';
  49. }
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5444KB
stdin
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
stdout
0 
1 0 
1 0 2 
0 3 2 1 
4 3 2 1 0 
0 3 2 1 5 4 
1 0 2 6 5 4 3 
1 0 7 6 5 4 3 2 
0 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1 0 
0 3 2 1 5 4 10 9 8 7 6 
4 3 2 1 0 11 10 9 8 7 6 5 
0 3 2 1 12 11 10 9 8 7 6 5 4 
1 0 2 13 12 11 10 9 8 7 6 5 4 3 
1 0 14 13 12 11 10 9 8 7 6 5 4 3 2 
0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 
1 0 7 6 5 4 3 2 17 16 15 14 13 12 11 10 9 8 
1 0 2 6 5 4 3 18 17 16 15 14 13 12 11 10 9 8 7 
0 3 2 1 5 4 19 18 17 16 15 14 13 12 11 10 9 8 7 6