Codeforces Round #427 (Div. 2) - B

本文解析了CodeForces上的一道题目,该题要求计算修改数字字符串中字符的最小次数,以确保所有数字之和不低于给定值k。通过贪心算法实现,先计算当前总和,再按需将数字替换为9来增加总和。

 

题目链接:http://codeforces.com/contest/835/problem/B

题意:给定一个数k和一个数字串n。问你最少改几个数字才能满足所有数字的和不小于k。

思路:考虑贪心,每次肯定把最小的数字改成'9' 会使得所有数字的和最大。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = 100000 + 24;
const int INF = 1e9;
const int mod = 1e9 + 7;
char n[MAXN];
int main(){
#ifdef kirito
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int k;
    while (~scanf("%d", &k)){
        scanf("%s", n);
        int sum = 0,len=strlen(n),res=0;
        for (int i = 0; i < len; i++){
            sum += (n[i] - '0');
        }
        sort(n, n + len);
        for (int i = 0; i < len&&sum<k; i++,res++){
            sum = (sum - (n[i] - '0') + 9);
        }
        printf("%d\n", res);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/kirito520/p/7271332.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值