forked from ACodeDaily/AutoTester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrect.cpp
More file actions
87 lines (83 loc) · 2.08 KB
/
correct.cpp
File metadata and controls
87 lines (83 loc) · 2.08 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
ll possible(const string &s, ll target, ll p, ll q) {
vector<ll> ps, qs;
for (ll i = 0; i <= target; i++) {
qs.push_back(s[i] - 'a');
ps.push_back(26 - (s[i] - 'a'));
}
sort(ps.begin(), ps.end());
sort(qs.begin(), qs.end());
ll completed = 0;
for (ll i = 0; i <= target; i++) {
if (ps[i] <= p) {
p -= ps[i];
completed++;
} else {
break;
}
}
ll total = target + 1;
ll remaining = total - completed;
for (ll i = 0; i <= target; i++) {
if (remaining == 0) {
break;
}
q -= qs[i];
remaining--;
}
return q;
}
ll possible2(const string &s, ll target, ll p) {
vector<ll> ps;
for (ll i = 0; i <= target; i++) {
ps.push_back(26 - (s[i] - 'a'));
}
sort(ps.begin(), ps.end());
ll completed = 0;
for (ll i = 0; i <= target; i++) {
if (ps[i] <= p) {
p -= ps[i];
completed++;
} else {
break;
}
}
return p;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n, p, q;
cin >> n >> p >> q;
string s;
cin >> s;
ll start = -1, end = n, mid;
while (start < end - 1) {
mid = start + (end - start) / 2;
if (possible(s, mid, p, q) >= 0)
start = mid;
else
end = mid;
}
ll remaining = possible(s, start, p, q);
ll remaining_p = possible2(s, start, p);
for (ll i = start + 2; i < n; i++) {
if (remaining_p >= 26 - (s[i] - 'a')) {
remaining_p -= (26 - (s[i] - 'a'));
s[i] = 'a';
}
}
if (start != n - 1)
s[start + 1] = (char) (s[start + 1] - remaining);
for (ll i = 0; i <= start; i++)
s[i] = 'a';
cout << s << endl;
}
return 0;
}