Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.
You have to find the minimum number of digits in which these two numbers can differ.
The first line contains integer k (1 ≤ k ≤ 109).
The second line contains integer n (1 ≤ n < 10100000).
There are no leading zeros in n. It's guaranteed that this situation is possible.
Print the minimum number of digits in which the initial number and n can differ.
3 11
1
3 99
0
In the first example, the initial number could be 12.
In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.
题目大意 问给定一个数,如果要使各位数字之和能够超过k,问至少要改动多少个数字。
从数字小的开始贪心。贪心地将它改成9.
Code
1 /** 2 * Codefores 3 * Problem#835B 4 * Accepted 5 * Time:16ms 6 * Memory:2100k 7 */ 8 #include <bits/stdc++.h> 9 #ifndef WIN32 10 #define Auto "%lld" 11 #else 12 #define Auto "%I64d" 13 #endif 14 using namespace std; 15 typedef bool boolean; 16 const signed int inf = (signed)((1u << 31) - 1); 17 const signed long long llf = (signed long long)((1ull << 61) - 1); 18 const double eps = 1e-6; 19 const int binary_limit = 128; 20 #define smin(a, b) a = min(a, b) 21 #define smax(a, b) a = max(a, b) 22 #define max3(a, b, c) max(a, max(b, c)) 23 #define min3(a, b, c) min(a, min(b, c)) 24 template<typename T> 25 inline boolean readInteger(T& u){ 26 char x; 27 int aFlag = 1; 28 while(!isdigit((x = getchar())) && x != '-' && x != -1); 29 if(x == -1) { 30 ungetc(x, stdin); 31 return false; 32 } 33 if(x == '-'){ 34 x = getchar(); 35 aFlag = -1; 36 } 37 for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0'); 38 ungetc(x, stdin); 39 u *= aFlag; 40 return true; 41 } 42 43 int k; 44 int sum = 0, len, res = 0; 45 char str[100005]; 46 int counter[10]; 47 48 inline void init() { 49 scanf("%d%s", &k, &str); 50 len = strlen(str); 51 memset(counter, 0, sizeof(counter)); 52 for(int i = 0; i < len; i++) 53 counter[str[i] - '0']++, sum += str[i] - '0'; 54 } 55 56 inline void solve() { 57 int fin = 0; 58 while(sum < k && fin < 9) { 59 while(counter[fin] == 0) fin++; 60 sum += 9 - fin, res++; 61 counter[fin]--; 62 } 63 if(fin == 9 && sum < k) res += ceil(k - sum * 1.0 / 9); 64 printf("%d ", res); 65 } 66 67 int main() { 68 init(); 69 solve(); 70 return 0; 71 }