CF118C.Fancy Number

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one.

Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one.

伯兰的汽车牌照号恰好由 nn 位数字组成。若一个牌照号中至少包含 kk 个相同的数字,则称其为“优美的”。瓦夏希望修改自己汽车牌照号中的若干位数字,使其变为优美的。将某一位数字从原数字改为新数字所需支付的金额,等于这两个数字之差的绝对值。

请帮助瓦夏:求出使其车牌号变为优美的最小总花费,并输出修改后得到的优美的车牌号。若存在多个满足条件的车牌号,则输出字典序最小的那个。

输入格式

The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits.

第一行包含两个以空格分隔的整数 nn 和 kk(2 ≤ n ≤ 1042 \leq n \leq 10^4,2 ≤ k ≤ n2 \leq k \leq n),分别表示该数字的位数,以及一个“优美数”中应具有的相同数字的个数。
第二行包含 nn 个数字,描述了瓦夏汽车的原车牌号。保证该数字中不含空格,且仅由数字组成。

输出格式

On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one.

第一行输出瓦夏更改数字所需的最少金额。
第二行输出汽车的新编号。如果存在多种解,输出字典序最小的一个。

输入输出样例

  • 输入#1

    6 5
    898196

    输出#1

    4
    888188
  • 输入#2

    3 2
    533

    输出#2

    0
    533
  • 输入#3

    10 6
    0001112223

    输出#3

    3
    0000002223

说明/提示

In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188".

The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that x__i < y__i, and for any j (1 ≤ j < i) x__j = y__j. The strings compared in this problem will always have the length n.

在第一个样例中,将第二位数字替换为“8”的代价为 ∣9 − 8∣ = 1|9 - 8| = 1;将第五位数字替换为“8”的代价同样为 11;将第六位数字替换为“8”的代价为 ∣6 − 8∣ = 2|6 - 8| = 2。因此,Vasya 为得到优美数字 “888188” 所需支付的总代价为 1 + 1 + 2 = 41 + 1 + 2 = 4。

现代编程语言中,字符串的字典序比较由 < 运算符实现。字符串 xx 字典序小于字符串 yy,当且仅当存在某个下标 ii(满足 1 ≤ i ≤ n1 \le i \le n),使得 xi < yix_i < y_i,且对任意 jj(满足 1 ≤ j < i1 \le j < i)都有 xj = yjx_j = y_j。本题中所比较的所有字符串长度均为 nn。

输入解题思路,AI测评打分。不知道怎么写?

首页