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;}
- CF835B-The number on the board
- The number on the board
- The number on the board
- Codeforces835B The number on the board
- Codeforces-The number on the board
- codeforces B. The number on the board
- B. The number on the board
- cfThe number on the board(水)
- Codeforces 835B-The number on the board
- Codeforces #835B: The Number on the Board 题解
- Codeforces 835 B The number on the board
- (贪心)Codeforces Round #427 B. The number on the board
- Codeforces 835 B The number on the board
- CodeForces 835B The number on the board
- codeforces 835b The number on the board
- CodeForces 835 B.The number on the board(贪心)
- codeforces 835B The number on the board
- Codeforces 835B The number on the board (贪心)
- 二分法求方程的根
- 火狐浏览器,改变你的浏览体验
- 博客开始打卡
- Spring MVC
- EXPRESS bodyParser
- CF835B-The number on the board
- jsp页面设置了IE版本Edge但是不生效
- Hi3519-pinctrl驱动开发②创建mapping table
- 值得学习的c语言开源项目
- word审阅功能、查看word文档的好姿势
- 【Java】Servlet基础及应用
- HDU 1465 不容易系列之一
- 【POJ 2407】Relatives(欧拉函数-水题)
- 红木家具之缅甸花梨家具