CF835B-The number on the board

来源:互联网 发布:java 获取文件路径 编辑:程序博客网 时间:2024/06/07 02:51

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.

题目大意:给出一个数n,利用替换将其各位之和大于等于k的最小替换数。
解题思路:贪心的思想,桶排序

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;const int maxnode=4000*100+10;const int MAXN=1e5+5;const int MAXM=1000;const int sigma_size=26;char s[MAXN];int cnt[15];int main(){    //cout<<40-8*2-7*2<<endl;    int k;    while(scanf("%d",&k)!=EOF)    {        scanf("%s",s);        mem(cnt,0);        int len=strlen(s);        int sum=0;        for(int i=0;i<len;++i)        {            sum+=(int)(s[i]-'0');            cnt[s[i]-'0']++;        }        //printf("%d\n",sum);        if(sum>=k) printf("0\n");        else        {            int ans=0,p=0;            sum=k-sum;            //printf("%d\n",sum);            int tmp=sum;            for(int i=0;i<=8;++i)            {                sum-=cnt[i]*(9-i);                if(sum<=0)  {p=i;break;}            }            //printf("%d\n",p);            if(p==0)            {                ans=tmp/9;                if(tmp%9!=0) ans++;                printf("%d\n",ans);            }else            {                for(int i=0;i<p;i++)                {                    tmp-=cnt[i]*(9-i);                    ans+=cnt[i];                }                //cout<<"lala "<<ans<<" "<<tmp<<endl;                ans+=tmp/(9-p);                if(tmp%(9-p)!=0) ans++;                printf("%d\n",ans);            }        }    }    return 0;}
原创粉丝点击