Submission #70829533
Source Code Expand
#include <bits/stdc++.h>
// #include "windows.h"
using namespace std;
const int MAXN = 1e6;
int T;
char a[MAXN + 5];
int p[30];
vector<int> shuffle(vector<int> vec) {
int p[30]; memset(p, 0, sizeof(p));
int n = vec.size();
if (n == 0) return {};
if (n == 1) return vec;
for (int i = 0; i < n; i ++)
p[vec[i]] ++;
int mx = 0;
for (int i = 1; i <= 26; i ++) {
mx = max(mx, p[i]);
}
if (mx <= (n + 1) / 2) {
vector<int> ans;
priority_queue<pair<int, int> > q;
for (int i = 1; i <= 26; i ++) {
if (p[i]) q.push({p[i], i});
}
int lst = 0;
while (!q.empty()) {
pair<int, int> tmp = q.top(); q.pop();
if (tmp.second != lst) {
ans.push_back(tmp.second);
lst = tmp.second;
tmp.first --;
if (tmp.first) q.push(tmp);
} else {
pair<int, int> tmp2 = q.top(); q.pop();
ans.push_back(tmp2.second);
lst = tmp2.second;
tmp2.first --;
if (tmp2.first) q.push(tmp2);
q.push(tmp);
}
}
return ans;
} else {
int ch;
vector<int> vec2;
for (int i = 1; i <= 26; i ++) {
if (p[i] == mx) {
ch = i;
continue;
}
for (int j = 1; j <= p[i]; j ++) vec2.push_back(i);
}
vec2 = shuffle(vec2);
vector<int> ans;
int t = mx / (n - mx + 1), lim = 0;
int t2 = t+1;
lim = (mx - t * (n - mx + 1));
if (lim > 0) {
for (int i = 1; i <= t2; i ++) ans.push_back(ch);
lim --;
} else {
for (int i = 1; i <= t; i ++) ans.push_back(ch);
}
for (int i = 1; i <= n - mx; i ++) {
ans.push_back(vec2.back());
vec2.pop_back();
if (lim > 0) {
for (int i = 1; i <= t2; i ++) ans.push_back(ch);
lim --;
} else {
for (int i = 1; i <= t; i ++) ans.push_back(ch);
}
}
return ans;
}
}
int main() {
cin >> T;
while (T --) {
scanf("%s", a + 1);
int n = strlen(a + 1);
vector<int> vec;
for (int i = 1; i <= n; i ++) vec.push_back(a[i] - 'a' + 1);
vec = shuffle(vec);
for (int i = 0; i < n; i ++) cout<<char('a'-1+vec[i]);cout<<endl;
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | B - Minimize Even Palindrome |
| User | laozhuma |
| Language | C++23 (GCC 15.2.0) |
| Score | 0 |
| Code Size | 2617 Byte |
| Status | WA |
| Exec Time | 6 ms |
| Memory | 7792 KiB |
Compile Error
./Main.cpp: In function 'int main()':
./Main.cpp:87:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
87 | for (int i = 0; i < n; i ++) cout<<char('a'-1+vec[i]);cout<<endl;
| ^~~
./Main.cpp:87:63: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
87 | for (int i = 0; i < n; i ++) cout<<char('a'-1+vec[i]);cout<<endl;
| ^~~~
In function 'construct_at',
inlined from 'construct' at /opt/atcoder/gcc/include/c++/15.2.0/bits/alloc_traits.h:676:21,
inlined from '_M_realloc_append' at /opt/atcoder/gcc/include/c++/15.2.0/bits/vector.tcc:586:26,
inlined from 'push_back' at /opt/atcoder/gcc/include/c++/15.2.0/bits/stl_vector.h:1427:21,
inlined from 'shuffle' at ./Main.cpp:62:56:
/opt/atcoder/gcc/include/c++/15.2.0/bits/stl_construct.h:110:16: warning: 'ch' may be used uninitialized [-Wmaybe-uninitialized]
110 | return ::new(__loc) _Tp(std::forward<_Args>(__args)...);
| ^
./Main.cpp: In function 'shuffle':
./Main.cpp:44:13: note: 'ch' was declared here
44 | int ch;
| ^
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 700 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample-01.txt |
| All | max-01.txt, max-02.txt, max-03.txt, mixed-01.txt, mixed-02.txt, mixed-03.txt, mixed-04.txt, mixed-05.txt, mixed-06.txt, mixed-07.txt, sample-01.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| max-01.txt | AC | 5 ms | 6896 KiB |
| max-02.txt | AC | 5 ms | 6872 KiB |
| max-03.txt | AC | 5 ms | 7792 KiB |
| mixed-01.txt | WA | 4 ms | 3664 KiB |
| mixed-02.txt | WA | 6 ms | 3780 KiB |
| mixed-03.txt | WA | 2 ms | 3660 KiB |
| mixed-04.txt | WA | 5 ms | 4452 KiB |
| mixed-05.txt | WA | 4 ms | 5904 KiB |
| mixed-06.txt | WA | 6 ms | 4744 KiB |
| mixed-07.txt | WA | 5 ms | 5724 KiB |
| sample-01.txt | AC | 1 ms | 3784 KiB |