不知道下面的代码哪里错了
/*
\ | ^ ^ \
-- | # # \
\_| \
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
const int N = 1e6 + 10;
int t;
int n;
int a[N];
int b[N];
int cnt[N];
std::vector<int> id[N];
void solve() {
std::cin >> n;
for (int i = 1; i <= n; ++i) {
cnt[i] = 0;
id[i].clear();
}
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
++cnt[a[i]];
id[cnt[a[i]]].push_back(i);
}
for (int i = 1; i <= n; ++i) {
if (id[i].size()) {
for (int j = 1; j < (int)id[i].size(); ++j) {
b[id[i][j - 1]] = a[id[i][j]];
}
b[id[i][(int)id[i].size() - 1]] = a[id[i][0]];
}
}
for (int i = 1; i <= n; ++i) {
std::cout << b[i] << ' ';
}
std::cout << '\n';
}
int main() {
std::ios::sync_with_stdio();
std::cin.tie(0), std::cout.tie(0);
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
输入 7 2 5 4 7 4 2
输出 2 5 4 7 4 2 7
感觉没有问题