A15091 | Sum of Substrings
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a binary string $s$ of length $n$ .
Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$ . In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$ .
For example, for the string $s = 1011$ :
- $d_1 = 10$ (ten);
- $d_2 = 01$ (one)
- $d_3 = 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.
Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$ . In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$ .
For example, for the string $s = 1011$ :
- $d_1 = 10$ (ten);
- $d_2 = 01$ (one)
- $d_3 = 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.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 10^5$ ). Description of the test cases follows.
First line of each test case contains two integers $n$ and $k$ ( $2 \le n \le 10^5$ , $0 \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 $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 $10^5$ .
First line of each test case contains two integers $n$ and $k$ ( $2 \le n \le 10^5$ , $0 \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 $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 $10^5$ .
输出格式
For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations.
输入输出样例
输入 #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$ .
- 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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted