【Codeforces】1230B - Ania and Minimizing

Problem Description:

Ania has a large integer S. Its decimal representation has length n and doesn't contain any leading zeroes. Ania is allowed to change at most k digits of S. She wants to do it in such a way that S still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?

Input Specification:

The first line contains two integers n and k (1≤n≤200000, 0≤k≤n) — the number of digits in the decimal representation of S and the maximum allowed number of changed digits.

The second line contains the integer S. It's guaranteed that SS has exactly n digits and doesn't contain any leading zeroes.

Output Specification:

Output the minimal possible value of S which Ania can end with. Note that the resulting integer should also have n digits.

Sample Input1:

5 3
51528

Sample Output1:

10028

Sample Input2:

3 2
102

Sample Output2:

100

Sample Input3:

1 1
1

Sample Output3:

0

Note:

A number has leading zeroes if it consists of at least two digits and its first digit is 0. For example, numbers 00, 00069 and 0101 have leading zeroes, while 0, 3000 and 1010 don't have leading zeroes.

解题思路:

题目大意就是给定一个n位数str,允许改变str上的k位,求能构成的最小的数是多少。分情况讨论:①若k=0说明str的一位数都不能改变,直接将str进行输出;②若n=1说明str只有个位数,直接将str置零进行输出;③n不为1且k不为0,先将str的第一位数置为1,然后再将剩下的位数全部置为0即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n,k;    //str的长度为n,允许改变str的k位数
    string str;
    cin >> n >> k >> str;
    if(!k)    //k=0说明str的一位都不能改变
    {
        cout << str << endl;
    }
    else if(n == 1)    //str只有个位数且k不为0则直接将str置零
    {
        cout << 0 << endl;
    }
    else
    {
        if(str[0] != '1')    //第一位数置1
        {
            str[0] = '1';
            k--;
        }
        Up(i,1,n-1)    //剩下的位全部置0
        {
            if(str[i]!='0' && k)
            {
                str[i] = '0';
                k--;
            }
        }
        cout << str << endl;
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢ctrl的cxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值