Codeforce 835B - The number on the board (贪心)
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.
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.
Print the minimum number of digits in which the initial number and n can differ.
3
11
1
3
99
0
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 如果总和比k小的话 就把差最大的改为9
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=;
const int mod=1e9+;
char a[N];
int main()
{
int k;
scanf("%d",&k);
scanf("%s",&a);
int len=strlen(a);
int s=;
for(int i=;i<len;i++){
s+=a[i]-'';
}
sort(a,a+len);
if(s>=k) cout<<<<endl;
else {
int t=;
for(int i=;i<len;i++){
s+=-(a[i]-'');
t++;
if(s>=k){
break;
}
}
cout<<t<<endl;
}
return ;
}
Codeforce 835B - The number on the board (贪心)的更多相关文章
- Codeforces Round #427 (Div. 2) B. The number on the board
引子: A题过于简单导致不敢提交,拖拖拉拉10多分钟还是决定交,太冲动交错了CE一发,我就知道又要错过一次涨分的机会.... B题还是过了,根据题意目测数组大小开1e5,居然蒙对,感觉用vector更 ...
- Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心
Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...
- CodeForce 359C Prime Number
Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1, ...
- cf201.div1 Number Transformation II 【贪心】
1 题目描述: 被给一系列的正整数x1,x2,x3...xn和两个非负整数a和b,通过下面两步操作将a转化为b: 1.对当前的a减1. 2.对当前a减去a % xi (i=1,2...n). 计算a转 ...
- codeforce 804B Minimum number of steps
cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each ...
- (Codeforce)The number of positions
Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say ...
- 【Codeforces Round #427 (Div. 2) B】The number on the board
[Link]:http://codeforces.com/contest/835 [Description] 原本有一个数字x,它的各个数码的和原本是>=k的; 现在这个数字x,在不改变位数的情 ...
- Largest Beautiful Number CodeForces - 946E (贪心)
题意:给定一个长度为偶数的数,输出小于它的最大的美丽数.如果一个数长度为偶数,且没有前导零,并存在一种排列是回文数的数为美丽数.给定的t个数长度总和不超过200000. 分析: 1.存在一种排列为回文 ...
- Display number of replies in disscussion board
how to display number of replies in disscussion board I have a require about display the replies' nu ...
随机推荐
- ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet' bytes怎么处理
今天ytkah进行了应急数据库恢复,用Navicat for Mysql导入sql文件出现ERROR 1153 (08S01): Got a packet bigger than 'max_allow ...
- Python3学习之路~3.3 内置函数
Python内置函数表: 内置参数详解:https://docs.python.org/3/library/functions.html?highlight=built#ascii 用法: #Auth ...
- 微信小程序 - 表单验证插件WxValidate使用
插件下载地址及官方文档:https://github.com/skyvow/wx-extend 具体的WxValidate.js文件的位置在wx-extend/src/assets/plugins/w ...
- 华为核心交换机绑定IP+MAC+端口案例
1 案例背景 某网络改造项目,核心交换机为华为S5700,接入交换机为不同型号交换机,如下模拟拓扑,客户端接入交换机1通过Access模式与核心交换机连接,该交换机下只有一个Vlan2 ...
- openshift 配置ldap认证
master主配置文件: ...... identityProviders: - challenge: true login: true mappingMethod: claim name: Ldap ...
- Tensorflow 搭建神经网络及tensorboard可视化
1. session对话控制 matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[2],[2]]) product = tf.matmul(m ...
- python regularexpress1
//test.py 1 import re 2 3 print (re.search('www', 'www.myweb.com').span()) 4 print (re.search('com', ...
- js判断手机是安卓还是ios
//点击下载按钮判断appDown() { var u = navigator.userAgent; var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac O ...
- python No tests were found问题解决方法
由于刚刚开始用python去写测试接口框架,在写的过程中遇到No tests were found问题 原因:python中unittest框架是以Test开头的方法,所以定义方法或类不能以Test开 ...
- sudo安装某一文件报错:E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?
报错原因:资源被占用 解决方法: sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock