Search for a command to run...
Hints
Open only as much as you need to keep the solve alive.
First simplify the definition. For what values of is ?
If , then . So the final number is beautiful exactly when its digit sum is in .
If the current digit sum is already at most , the answer is . Otherwise, increasing digits is useless; you only need to lower the digit sum.
Changing a non-leading digit can reduce the sum by at most . Changing the leading digit can reduce it by at most , because it cannot become $0`.
Let the needed reduction be . Sort all maximum possible reductions and take the largest ones until their sum reaches .
Research note. I checked the official statement, the Educational Codeforces Round 187 editorial page, which currently exposes the reference solution more than prose, a Codeforces accepted-status page, and a public implementation for the same problem at GitHub. The supplied statement is canonical; these sources just confirm the greedy direction and the leading-digit edge case.
Let be the digit sum of the current number. The beauty condition is
so it is really asking when .
For every , is one digit, so . For every , the digit sum is strictly smaller than the number itself, so . Therefore a number is beautiful exactly when its digit sum is between and . That is the whole trick; the scary-looking double is just a fake mustache.
If the original digit sum , we are already done and the answer is .
Now assume . We need to reduce the digit sum to at most , so the needed reduction is
Consider one digit. If it is not the leading digit and its value is , we can change it to , reducing the sum by at most . If it is the leading digit, we cannot change it to , so the smallest allowed value is , and the maximum reduction is .
So each digit has a capacity:
Changing digit can contribute any reduction from up to , as long as we pick the replacement digit accordingly.
Now the problem is: what is the minimum number of capacities whose total can cover ?
The greedy answer is to sort capacities descending and take the biggest ones first.
Why is this optimal? For any fixed number of moves , the maximum possible digit-sum reduction is the sum of the largest capacities. No other set of digits can do better. Therefore, if the largest capacities sum to less than , then moves are impossible. The first where their sum reaches at least is also achievable: reduce the first chosen digits as much as possible, then reduce the last chosen digit by exactly the remaining amount. Since the previous were not enough, that remaining amount is positive and at most the last digit's capacity.
The final digit sum can be made exactly , so it is beautiful. No overshooting drama needed.
Edge cases:
Complexity per test case is for sorting the digits, where . Memory is .
#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--) {
string x;
cin >> x;
int sum = 0;
vector<int> reduce;
for (int i = 0; i < (int)x.size(); i++) {
int d = x[i] - '0';
sum += d;
reduce.push_back(d - (i == 0));
}
int need = max(0, sum - 9);
sort(reduce.rbegin(), reduce.rend());
int ans = 0, got = 0;
for (int c : reduce) {
if (got >= need) break;
got += c;
ans++;
}
cout << ans << '\n';
}
}#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--) {
string x;
cin >> x;
int sum = 0;
vector<int> reduce;
for (int i = 0; i < (int)x.size(); i++) {
int d = x[i] - '0';
sum += d;
reduce.push_back(d - (i == 0));
}
int need = max(0, sum - 9);
sort(reduce.rbegin(), reduce.rend());
int ans = 0, got = 0;
for (int c : reduce) {
if (got >= need) break;
got += c;
ans++;
}
cout << ans << '\n';
}
}