CodeForces - 835B 【贪心】

B. The number on the board
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
Copy
3
11
output
Copy
1
input
Copy
3
99
output
Copy
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.

这道题就是一个简单的贪心,结果比赛的时候我理解错题意了,误认为是任意两位上的数相加至少大于k,然后一口气错了7次。。。。。。。。

下来听同学讨论的时候才意识到自己理解错了心里一阵mmp........。

这道题的大致思路就是,字符串输入s然后计算每一位相加的总和sum,再然后给字符串由小到大排序,一次把每一位的数换成最大的9然后再让sun和k进行比较,如果sum<k则继续替换,个数+1,否则跳出循环结束比较。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(char a,char b){
    return a<b;
}
int main(){
    int k;
    while(~scanf("%d",&k)){
        char str[1000010];
        scanf("%s",str);
        int sum=0;
        int len=strlen(str);
        for(int i=0;i<len;i++){
            sum=sum+str[i]-'0';
        }
        if(sum>=k) printf("0\n");
        else{
            int ans=0;
            sort(str,str+len,cmp);
            for(int i=0;i<len;i++){
                sum=sum-(str[i]-'0')+9;
                if(sum<k) ans++;
                else{
                    ans=ans+1;
                    break;
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiang_hehe/article/details/80565976