A16435 | Swap and Delete
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a binary string $s$ (a string consisting only of 0-s and 1-s).
You can perform two types of operations on $s$ :
1. delete one character from $s$ . This operation costs $1$ coin;
2. swap any pair of characters in $s$ . This operation is free (costs $0$ coins).
You can perform these operations any number of times and in any order.
Let's name a string you've got after performing operations above as $t$ . The string $t$ is good if for each $i$ from $1$ to $|t|$ $t_i \neq s_i$ ( $|t|$ is the length of the string $t$ ). The empty string is always good. Note that you are comparing the resulting string $t$ with the initial string $s$ .
What is the minimum total cost to make the string $t$ good?
You can perform two types of operations on $s$ :
1. delete one character from $s$ . This operation costs $1$ coin;
2. swap any pair of characters in $s$ . This operation is free (costs $0$ coins).
You can perform these operations any number of times and in any order.
Let's name a string you've got after performing operations above as $t$ . The string $t$ is good if for each $i$ from $1$ to $|t|$ $t_i \neq s_i$ ( $|t|$ is the length of the string $t$ ). The empty string is always good. Note that you are comparing the resulting string $t$ with the initial string $s$ .
What is the minimum total cost to make the string $t$ good?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases. Then $t$ test cases follow.
The only line of each test case contains a binary string $s$ ( $1 \le |s| \le 2 \cdot 10^5$ ; $s_i \in \{$ 0, 1 $\}$ ) — the initial string, consisting of characters 0 and/or 1.
Additional constraint on the input: the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$ .
The only line of each test case contains a binary string $s$ ( $1 \le |s| \le 2 \cdot 10^5$ ; $s_i \in \{$ 0, 1 $\}$ ) — the initial string, consisting of characters 0 and/or 1.
Additional constraint on the input: the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$ .
输出格式
For each test case, print one integer — the minimum total cost to make string $t$ good.
输入输出样例
输入 #1
4 0 011 0101110001 111100
输出 #1
1 1 0 4
In the first test case, you have to delete a character from $s$ to get the empty string $t$ . Only then $t$ becomes good. One deletion costs $1$ coin.
In the second test case, you can, for example, delete the second character from $s$ to get the string 01, and then swap the first and second characters to get the string $t$ $=$ 10. String $t$ is good, since $t_1 \neq s_1$ and $t_2 \neq s_2$ . The total cost is $1$ coin.
In the third test case, you can, for example, swap $s_1$ with $s_2$ , swap $s_3$ with $s_4$ , swap $s_5$ with $s_7$ , $s_6$ with $s_8$ and $s_9$ with $s_{10}$ . You'll get $t$ $=$ 1010001110. All swap operations are free, so the total cost is $0$ .
In the second test case, you can, for example, delete the second character from $s$ to get the string 01, and then swap the first and second characters to get the string $t$ $=$ 10. String $t$ is good, since $t_1 \neq s_1$ and $t_2 \neq s_2$ . The total cost is $1$ coin.
In the third test case, you can, for example, swap $s_1$ with $s_2$ , swap $s_3$ with $s_4$ , swap $s_5$ with $s_7$ , $s_6$ with $s_8$ and $s_9$ with $s_{10}$ . You'll get $t$ $=$ 1010001110. All swap operations are free, so the total cost is $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted