(贪心)Codeforces Round #427 B. The number on the board
来源:互联网 发布:lol网络关注 编辑:程序博客网 时间:2024/06/06 01:19
题目网址: Codeforces Round #427 B. The number on the board
题意分析:
题意:
- board上写着一串数字,该数字所有数位和大于等于k
- 但此时某人修改了这串数字的某些数位, 使得这串数字变成了n
- 求原来这串数字与n最少有多少个不同的数位
思路:
- 先计算出n数位和cnt
- 若cnt >= k, 那么最少不同数位可以为 0
- 如果cnt < k, 计算出这个数与k还相差多少 tmp = k - cnt
- 贪心, 为了使得最少数位不同, 则尽可能找小的数位来弥补cnt与k的差距
- 所以对n里面的字符排序(从小到大), 然后 每次 tmp -= (9 - n[i] + ‘0’), (尽可能使用小的数位来弥补差距)
代码:
#include <bits/stdc++.h>using namespace std;int main(int argc, char const *argv[]){ string n; int k; while (~scanf("%d", &k)) { cin >> n; int len = (int)n.length(); int cnt = 0; for (int i = 0; i < len; ++i) { cnt += (n[i] - '0'); } if (cnt >= k) printf("0\n"); else { sort(n.begin(), n.end()); int tmp = k - cnt; int i = 0; int ans = 0; while (i < len && tmp > 0) { tmp -= (9 - n[i] + '0'); ++i; ++ans; } printf("%d\n", ans); } } return 0;}
阅读全文
0 0
- (贪心)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 B. The number on the board
- Codeforces Round #427 (Div. 2) B. The number on the board
- Codeforces Round #427 (Div. 2)-B. The number on the board
- Codeforces Round #427 (Div. 2) B. The number on the board
- Codeforces Round #427 (Div. 2)B. The number on the board
- codeforces 835B The number on the board (优先队列,贪心)
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- Codeforces 835B-The number on the board
- Codeforces #835B: The Number on the Board 题解
- Codeforces 835 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 835B The number on the board
- codeforces 835b 之 The number on the board
- Java中关键字的大致含义
- mybatis基础--作用域和生命周期
- 《javascrip编程精解》第二版习题练习(未完,根据自己学习进度更新)
- Android中static关键字的理解,及几个面试点
- 自定义控件三部曲之动画篇(八)——PropertyValuesHolder与Keyframe
- (贪心)Codeforces Round #427 B. The number on the board
- Light oj 1104 Birthday Paradox 生日悖论-雀巢原理
- HDU 6040 Hints of sd0061 找第K大数 (nth_element函数)
- js里添加html调用function参数问题
- 模拟天猫--网页定位导航效果
- Linux中三种网络模式是什么意思——NAT(二地址转换模式)
- CodeForces 803C
- LeetCode -- 198. House Robber
- PHP与表单的简单应用