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.
Think about what happens to all positions that get the same value in . If , then every position with value sees the exact same occurrence count: .
So positions with different values can never share the same value in . If one group of equal has size , then every index inside that group must have .
For a fixed number , look only at indices where . These indices must be split into groups of exactly indices, because each constructed value appearing there must appear exactly times.
That means the count of indices with must be divisible by . If not, there is leftover garbage you cannot place anywhere, so the answer is .
If every count is divisible, construct directly: for each , take its indices in chunks of size , assign a fresh value to each chunk, and move on. Fresh labels from to are enough.
We need build an array such that for every position , the number of times appears in the whole array equals .
The key is to stop thinking about values one position at a time. Values in create groups.
If some value appears in positions:
then , so every one of those positions must satisfy:
So a value that appears times can only be assigned to positions whose is exactly .
That gives the whole problem away.
Necessary condition
For each possible value , count how many positions want occurrence count :
Those positions must be split into groups of size exactly .
So we need:
for every .
If this fails for even one , the answer is impossible. No clever construction can fix it; you cannot make a group of size . Math is annoying like that.
Sufficient condition
If every is divisible by , construction is easy:
For example, if the indices with are:
then split them as:
Assign one new value to the first chunk, and another new value to the second chunk. Each of those values appears exactly times, so every index in those chunks correctly has occurrence count .
Why labels from to are enough
Every group gets one fresh label. The total number of groups is:
Since , this is at most:
So we never need more than distinct labels, and the statement allows all between and .
Proof of correctness
For each , the algorithm only groups indices whose . Each group has exactly indices and receives a unique value. Therefore that value appears exactly times in .
Now take any position . Suppose . The algorithm placed into some chunk of size , and assigned that entire chunk one fresh value . Since appears exactly on that chunk and nowhere else, . Therefore:
So every position satisfies the requirement.
If the algorithm prints , then for some , is not divisible by . But every value assigned to positions with would need to appear exactly times, meaning those positions must be partitioned into groups of size . That is impossible when is not divisible by . So printing is correct.
Complexity
Each index is stored and assigned once.
Time complexity: per test case.
Memory complexity: per test case.
#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;
vector<vector<int>> pos(n + 1);
for (int i = 0; i < n; i++) {
int b;
cin >> b;
pos[b].push_back(i);
}
vector<int> ans(n, 0);
int label = 1;
bool ok = true;
for (int k = 1; k <= n; k++) {
if ((int)pos[k].size() % k != 0) {
ok = false;
break;
}
for (int i = 0; i < (int)pos[k].size(); i += k) {
for (int j = i; j < i + k; j++) {
ans[pos[k][j]] = label;
}
label++;
}
}
if (!ok) {
cout << -1 << '\n';
} else {
for (int i = 0; i < n; i++) {
cout << ans[i] << (i + 1 == n ? '\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;
vector<vector<int>> pos(n + 1);
for (int i = 0; i < n; i++) {
int b;
cin >> b;
pos[b].push_back(i);
}
vector<int> ans(n, 0);
int label = 1;
bool ok = true;
for (int k = 1; k <= n; k++) {
if ((int)pos[k].size() % k != 0) {
ok = false;
break;
}
for (int i = 0; i < (int)pos[k].size(); i += k) {
for (int j = i; j < i + k; j++) {
ans[pos[k][j]] = label;
}
label++;
}
}
if (!ok) {
cout << -1 << '\n';
} else {
for (int i = 0; i < n; i++) {
cout << ans[i] << (i + 1 == n ? '\n' : ' ');
}
}
}
return 0;
}