Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The distance does not need to be exactly . Any multiple of is valid, so distances like are totally legal.
Try putting most numbers once in decreasing order, then again in increasing order. Track where a number lands.
If the first block is , then number first appears at position .
If after that you print another , then print , the second copy of appears at position .
For , the distance becomes . For , the two copies are at positions and , distance . Done.
The main trap is assuming we need to place the two copies of exactly apart. We don't. The statement only asks for a multiple of , so , , , etc. are all fine.
That extra freedom makes the construction almost stupidly simple.
Use this array:
So we print:
This gives exactly elements.
Checking occurrences
First, every number from to appears exactly twice:
Now check the distances.
For :
The distance is:
which is divisible by .
For any :
In the decreasing block , the number is at position:
In the increasing suffix , the number appears after the first positions, so its position is:
The distance is:
And is obviously divisible by . No magic, no casework, no cursed backtracking. Just exploit the word “multiple”.
Complexity
For each test case we print numbers, so the time complexity is and memory usage is besides output buffering.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = n; i >= 1; --i) {
cout << i << ' ';
}
cout << n;
for (int i = 1; i < n; ++i) {
cout << ' ' << i;
}
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = n; i >= 1; --i) {
cout << i << ' ';
}
cout << n;
for (int i = 1; i < n; ++i) {
cout << ' ' << i;
}
cout << '\n';
}
return 0;
}