-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe.cpp
110 lines (106 loc) · 1.76 KB
/
e.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int T, N, pfx[10], sfx[10];
string s;
string sub1(string c, int cL){
int carry = 1;
for(int i = cL-1; i >= 0; i--){
c[i] -= carry;
carry = 0;
if(c[i] < '0'){
carry = 1;
c[i] = '9';
}
}
return c;
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> s;
fill(pfx, pfx+10, 0);
fill(sfx, sfx+10, 0);
N = s.length();
s = sub1(s, N);
if(sub1(s, N)[0] == '0'){
for(int i = 0; i < N-2; i++){
moo << 9;
}
moo << endl;
continue;
}
for(int i = 0; i < N; i++){
pfx[s[i]-'0']++;
}
bool wow = 1;
for(int i = 0; i < 10; i++){
if(pfx[i] & 1){
wow = 0;
break;
}
}
if(wow){
moo << s << endl;
continue;
}
string ans = "";
for(int i = N-1; i >= 0; i--){
pfx[s[i]-'0']--;
int nbad = 0;
bool hasbl = 0;
for(int j = 0; j < 10; j++){
if(pfx[j] & 1){
if(j < s[i]-'0' && !hasbl){
hasbl = 1;
continue;
}
nbad++;
}
}
int rem = N-1-i;
if(rem < nbad || s[i] == '0'){
continue;
}
for(int j = 0; j < i; j++){
moo << s[j];
}
int lstd = s[i]-'0'-1;
for(; lstd >= 0; lstd--){
int baddies = 0;
pfx[lstd]++;
for(int j = 0; j < 10; j++){
if(pfx[j] & 1){
baddies++;
}
}
if(baddies <= rem){
break;
}
pfx[lstd]--;
}
moo << lstd;
string tag = "";
for(int j = i+1; j < N; j++){
bool miss = false;
for(int k = 0; k < 10; k++){
if(pfx[k] & 1){
miss = true;
tag += (char)('0' + k);
pfx[k]--;
break;
}
}
if(miss) continue;
tag += '9';
}
reverse(tag.begin(), tag.end());
moo << tag << endl;
break;
}
}
}