CF1691C.Sum of Substrings
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary string s of length n.
Let's define di as the number whose decimal representation is sisi+1 (possibly, with a leading zero). We define f(s) to be the sum of all the valid di. In other words, f(s)=i=1∑n−1di.
For example, for the string s=1011:
- d1=10 (ten);
- d2=01 (one)
- d3=11 (eleven);
- f(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) that can be achieved if at most k operations are allowed.
给你一个长度为 n 的二进制字符串 s。
定义 di 为由十进制数字 sisi+1(可能含前导零)所表示的数。我们定义 f(s) 为所有合法的 di 之和,即 f(s)=i=1∑n−1di。
例如,对于字符串 s=1011:
- d1=10(十);
- d2=01(一);
- d3=11(十一);
- f(s)=10+01+11=22。
每次操作可以交换字符串中任意两个相邻元素。在最多进行 k 次操作的前提下,求所能达到的 f(s) 的最小值。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105). Description of the test cases follows.
First line of each test case contains two integers n and k (2≤n≤105, 0≤k≤109) — the length of the string and the maximum number of operations allowed.
The second line of each test case contains the binary string s of length n, consisting of only zeros and ones.
It is also given that sum of n over all the test cases doesn't exceed 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤105)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 k(2≤n≤105,0≤k≤109)——分别表示字符串的长度和允许执行的最大操作次数。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s,该字符串仅由字符 0 和 1 组成。
此外,保证所有测试用例的 n 值之和不超过 105。
输出格式
For each test case, print the minimum value of f(s) you can obtain with at most k operations.
对于每个测试用例,输出在最多执行 k 次操作的情况下所能得到的 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 s itself. f(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 f value of 22.
-
For the third example, one of the optimal strings you can obtain is "00011". The string has an f value of 12.
-
对于第一个样例,你无法执行任何操作,因此最优字符串即为 s 本身。f(s)=f(1010)=10+01+10=21。
-
对于第二个样例,你可以得到的最优字符串之一是 “0011000”。该字符串的 f 值为 22。
-
对于第三个样例,你可以得到的最优字符串之一是 “00011”。该字符串的 f 值为 12。
输入解题思路,AI测评打分。不知道怎么写?