Codeforces Beta Round #89 (Div. 2) C. Fancy Number

本文介绍了一种算法问题——如何将车牌号变为“美丽”的最小成本解决方案。文章详细解释了问题背景,即通过替换车牌上的某些数字来形成至少k个相等的数字,并确保花费最少。文中还提供了具体的样例输入输出,以及解决问题的策略:先统计数字频率,再通过枚举确定最优解,以确保修改后的车牌号码字典序最小。
部署运行你感兴趣的模型镜像
C. Fancy Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A car number in Berland consists of exactlyndigits. A number is called beautiful if it has at leastkequal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one ofndigits 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.

Input

The first line contains two space-separated integersnandk(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 ofndigits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits.

Output

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.

Sample test(s)
input
6 5
898196
output
4
888188
input
3 2
533
output
0
533
input
10 6
0001112223
output
3
0000002223
Note

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 pay1 + 1 + 2 = 4for a beautiful number "888188".

The lexicographical comparison of strings is performed by the < operator in modern programming languages. The stringxis lexicographically smaller than the stringy, if there exists suchi(1 ≤ i ≤ n), thatxi < yi, and for anyj(1 ≤ j < i)xj = yj. The strings compared in this problem will always have the lengthn.



这道题明显是YY题,不过应该很好想到。

先把0-9每个数的个数统计出来,然后就是枚举0-9了

枚举的时候是枚举绝对值大小,把i从1到9枚举,设绝对值大小为t, 那么原串中要被换掉的数就为i+t和i-t,那么优先换谁好呢,如果换i+t,显然原串的字典序会变小,换i-t就会使字典序增大,所以要优先换掉i+t,而且,换i+t时要从左往右换,这样会使原串尽可能的变小,而换i-t时,就要从右向左换,这样,就会使原串的尽量少的变大。

代码就不写了,应该很好写

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值