codeforces 835b The number on the board
2017-08-02 20:40
281 查看
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
output
input
output
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.
给出一个数字,再给出一个数,问最少将下面的数字多少位的数字换掉能够满足每一位的数字求和能够大于等于第一个给出的数字。
贪心,先求和看是不是一开始就超过了,若未超过则将每一位数字从小到大排序,再从小到大开始让9减掉每一位的数字,将差加到求和里面去,一旦超过第一个数字则结束循环的次数就是答案。
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
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.
给出一个数字,再给出一个数,问最少将下面的数字多少位的数字换掉能够满足每一位的数字求和能够大于等于第一个给出的数字。
贪心,先求和看是不是一开始就超过了,若未超过则将每一位数字从小到大排序,再从小到大开始让9减掉每一位的数字,将差加到求和里面去,一旦超过第一个数字则结束循环的次数就是答案。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> #include<map> #include<vector> #include<queue> #include<stack> using namespace std; int main(){ char a[1111111]; long long int k; while(cin>>k>>a) { sort(a,a+strlen(a)); int i; long long int sum=0; for(i=0;i<strlen(a);i++) sum+=a[i]-'0'; if(sum>=k)cout<<"0"<<endl; else { long long int ans=0; for(i=0;i<strlen(a);i++) { if(sum>=k)break; sum+='9'-a[i]; ans++; } cout<<ans<<endl; } } return 0; }
相关文章推荐
- CodeForces 764B Timofey and cubes
- COdeforces 835E The penguin's game (二进制)
- codeforces 3B Lorry
- Codeforces 570D - Tree Requests【树形转线性,前缀和】
- 小朋友学Codeforces(2):Round 454 DIV 2, A
- Codeforces 653B Bear and Compressing【DFS】
- Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)
- Codeforces 131A - cAPS lOCK
- CodeForces - 813C The Tag Game —— BFS DFS
- CodeForces 339A Helpful Maths
- codeforces 343A Rational Resistance
- Codeforces-935C:Fifa and Fafa
- CodeForces 452C - Magic Trick-概率
- Codeforces 707B. Bakery
- CodeForces - 544B Sea and Islands
- CodeForces540E【树状数组+二分】
- codeforces April Fools Day Contest 2014 A. The Great Game
- Codeforces 474 A
- Codeforces 939F.Cutlet-单调队列优化dp
- Codeforces 161D Distance in Tree(树的点分治)