CF1913B.Swap and Delete

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a binary string ss (a string consisting only of 0-s and 1-s).

You can perform two types of operations on ss:

  1. delete one character from ss. This operation costs 11 coin;
  2. swap any pair of characters in ss. This operation is free (costs 00 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 tt. The string tt is good if for each ii from 11 to ∣t∣|t| ti≠sit_i \neq s_i (∣t∣|t| is the length of the string tt). The empty string is always good. Note that you are comparing the resulting string tt with the initial string ss.

What is the minimum total cost to make the string tt good?

给你一个二进制字符串 ss(仅由字符 0 和 1 组成的字符串)。

你可以在 ss 上执行以下两种操作:

  1. 删除 ss 中的一个字符,该操作花费 11 枚硬币;
  2. 交换 ss 中任意两个字符,该操作免费(花费 00 枚硬币)。

你可以以任意顺序、任意次数执行上述操作。

将执行若干操作后得到的字符串记为 tt。若对每个 ii(从 11 到 ∣t∣|t|),均有 ti≠sit_i \neq s_i(其中 ∣t∣|t| 表示字符串 tt 的长度),则称字符串 tt 是好的。空字符串总是好的。注意:此处是将最终得到的字符串 tt 与初始字符串 ss 进行逐位比较。

使字符串 tt 成为好的字符串所需的最小总花费是多少?

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Then tt test cases follow.

The only line of each test case contains a binary string ss (1≤∣s∣≤2⋅1051 \le |s| \le 2 \cdot 10^5; $s_i \in {0,10, 1}$) — the initial string, consisting of characters 0 and/or 1.

Additional constraint on the input: the total length of all strings ss doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。随后是 tt 个测试用例。

每个测试用例仅有一行,包含一个二进制字符串 ss(1≤∣s∣≤2⋅1051 \le |s| \le 2 \cdot 10^5;si∈{0,1}s_i \in \{0, 1\})—— 由字符 0 和/或 1 组成的初始字符串。

输入的额外约束:所有字符串 ss 的总长度不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print one integer — the minimum total cost to make string tt good.

对于每个测试用例,输出一个整数——使字符串 tt 变为“好字符串”的最小总代价。

输入输出样例

  • 输入#1

    4
    0
    011
    0101110001
    111100

    输出#1

    1
    1
    0
    4

说明/提示

In the first test case, you have to delete a character from ss to get the empty string tt. Only then tt becomes good. One deletion costs 11 coin.

In the second test case, you can, for example, delete the second character from ss to get the string 01, and then swap the first and second characters to get the string tt == 10. String tt is good, since t1≠s1t_1 \neq s_1 and t2≠s2t_2 \neq s_2. The total cost is 11 coin.

In the third test case, you can, for example, swap s1s_1 with s2s_2, swap s3s_3 with s4s_4, swap s5s_5 with s7s_7, s6s_6 with s8s_8 and s9s_9 with s10s_{10}. You'll get tt == 1010001110. All swap operations are free, so the total cost is 00.

在第一个测试用例中,你需要从 ss 中删除一个字符以得到空字符串 tt,此时 tt 才成为“好”字符串。一次删除操作花费 11 枚硬币。

在第二个测试用例中,例如,你可以从 ss 中删除第二个字符,得到字符串 01,然后交换第一个与第二个字符,从而得到字符串 t=10t = 10。字符串 tt 是“好”的,因为 t1≠s1t_1 \neq s_1 且 t2≠s2t_2 \neq s_2。总花费为 11 枚硬币。

在第三个测试用例中,例如,你可以交换 s1s_1 与 s2s_2、s3s_3 与 s4s_4、s5s_5 与 s7s_7、s6s_6 与 s8s_8,以及 s9s_9 与 s10s_{10},最终得到 t=1010001110t = 1010001110。所有交换操作均免费,因此总花费为 00。

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

首页