fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24. void consistency(int n) {
  25.  
  26. int totalSum = n*(n+1)/2;
  27. if(totalSum & 1){
  28. cout << -1 << endl;
  29. return;
  30. }
  31.  
  32. int halfSum = totalSum/2;
  33.  
  34. vector<int> subset;
  35. int sum = 0;
  36. int i=1;
  37. while(sum + i <= halfSum){
  38. sum += i;
  39. subset.push_back(i);
  40. i++;
  41. }
  42.  
  43. int last = subset.back();
  44. subset.pop_back();
  45. sum -= last;
  46.  
  47. subset.push_back(n);
  48. sum += n;
  49.  
  50. vector<int> a, b;
  51.  
  52. b.push_back(last);
  53. while(i<n){
  54. b.push_back(i);
  55. i++;
  56. }
  57.  
  58. int remove = sum - halfSum;
  59. for(int i=0; i<subset.size(); i++){
  60. if(subset[i] == remove ){
  61. b.push_back(subset[i]);
  62. }
  63. else {
  64. a.push_back(subset[i]);
  65. }
  66. }
  67.  
  68. sort(begin(b), end(b));
  69.  
  70. vector<int> p;
  71. for(int i=0; i<a.size(); i++){
  72. cout << a[i] << " ";
  73. }
  74. cout << " | ";
  75. for(int i=0; i<b.size(); i++){
  76. cout << b[i] << " ";
  77. }
  78. cout << endl;
  79.  
  80. }
  81.  
  82.  
  83. void solve() {
  84.  
  85. int n;
  86. cin >> n ;
  87.  
  88. consistency(n);
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. int32_t main() {
  97. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  98.  
  99. int t = 1;
  100. cin >> t;
  101. while (t--) {
  102. solve();
  103. }
  104.  
  105. return 0;
  106. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
8
11
6
12
stdout
1 2 3 4 8  |  5 6 7 
1 2 3 4 5 6 11  |  7 8 9 10 
-1
2 3 4 5 6 7 12  |  1 8 9 10 11