CF1691C.Sum of Substrings

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a binary string ss of length nn.

Let's define did_i as the number whose decimal representation is sisi+1s_i s_{i+1} (possibly, with a leading zero). We define f(s)f(s) to be the sum of all the valid did_i. In other words, f(s)=∑i=1n−1dif(s) = \sum\limits_{i=1}^{n-1} d_i.

For example, for the string s=1011s = 1011:

  • d1=10d_1 = 10 (ten);
  • d2=01d_2 = 01 (one)
  • d3=11d_3 = 11 (eleven);
  • f(s)=10+01+11=22f(s) = 10 + 01 + 11 = 22.

In one operation you can swap any two adjacent elements of the string. Find the minimum value of f(s)f(s) that can be achieved if at most kk operations are allowed.

给你一个长度为 nn 的二进制字符串 ss。

定义 did_i 为由十进制数字 sisi+1s_i s_{i+1}(可能含前导零)所表示的数。我们定义 f(s)f(s) 为所有合法的 did_i 之和,即 f(s)=∑i=1n−1dif(s) = \sum\limits_{i=1}^{n-1} d_i。

例如,对于字符串 s=1011s = 1011:

  • d1=10d_1 = 10(十);
  • d2=01d_2 = 01(一);
  • d3=11d_3 = 11(十一);
  • f(s)=10+01+11=22f(s) = 10 + 01 + 11 = 22。

每次操作可以交换字符串中任意两个相邻元素。在最多进行 kk 次操作的前提下,求所能达到的 f(s)f(s) 的最小值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). Description of the test cases follows.

First line of each test case contains two integers nn and kk (2≤n≤1052 \le n \le 10^5, 0≤k≤1090 \le k \le 10^9) — the length of the string and the maximum number of operations allowed.

The second line of each test case contains the binary string ss of length nn, consisting of only zeros and ones.

It is also given that sum of nn over all the test cases doesn't exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1051 \le t \le 10^5)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(2≤n≤1052 \le n \le 10^5,0≤k≤1090 \le k \le 10^9)——分别表示字符串的长度和允许执行的最大操作次数。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss,该字符串仅由字符 0 和 1 组成。

此外,保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, print the minimum value of f(s)f(s) you can obtain with at most kk operations.

对于每个测试用例,输出在最多执行 kk 次操作的情况下所能得到的 f(s)f(s) 的最小值。

输入输出样例

  • 输入#1

    3
    4 0
    1010
    7 1
    0010100
    5 2
    00110

    输出#1

    21
    22
    12

说明/提示

  • For the first example, you can't do any operation so the optimal string is ss itself. f(s)=f(1010)=10+01+10=21f(s) = f(1010) = 10 + 01 + 10 = 21.

  • For the second example, one of the optimal strings you can obtain is "0011000". The string has an ff value of 2222.

  • For the third example, one of the optimal strings you can obtain is "00011". The string has an ff value of 1212.

  • 对于第一个样例,你无法执行任何操作,因此最优字符串即为 ss 本身。f(s)=f(1010)=10+01+10=21f(s) = f(1010) = 10 + 01 + 10 = 21。

  • 对于第二个样例,你可以得到的最优字符串之一是 “0011000”。该字符串的 ff 值为 2222。

  • 对于第三个样例,你可以得到的最优字符串之一是 “00011”。该字符串的 ff 值为 1212。

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

首页