http://www.elijahqi.win/archives/425
B. The number on the board
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
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.
Output
Print the minimum number of digits in which the initial number and n can differ.
Examples
Input
3
11
Output
1
Input
3
99
Output
0
Note
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.
标准贪心,但却困扰我好几个小时,想来也是自己zz.题意首先要确定,就是在给定n的基础上满足最小改动几位能使n的数位和≥k
#include<cstdio>
int k,n;
int a[110000],f[110000];
int main(){
freopen("cfb.in","r",stdin);
int ans=0,num=0;
scanf("%d",&k);char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0') a[++num]=ch-'0',ch=getchar(),f[a[num]]++;
for (int i=1;i<=num;++i) ans+=a[i];
if (ans>=k){
printf("0");return 0;
}
int tmp=k-ans,num1=0;
for (int i=0;i<=8;++i){
if (tmp>=0){
while (f[i]>0&&tmp>0) num1++,tmp-=9-i,f[i]--;
}else break;
}
if (tmp>0) num1++;
printf("%d",num1);
return 0;
}