CF2189E.Majority Wins?

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a binary string∗^{\text{∗}} ss of length nn.

In one operation with a binary string gg of length kk, you can do the following:

  • choose some l,rl, r such that 1≤l≤r≤k1 \leq l \leq r \leq k;

  • replace the substring†^{\text{†}} gl,…,grg_l, \ldots, g_r of the string gg with one character that appears in this substring at least as many times as the other character.

The cost of such an operation will be equal to r−l+1r-l+1.

For example, the string 010010 can be transformed into 010 in one operation with a cost of 44 by replacing the substring 1001 with 1; the string 1111 can be transformed into 1 by performing an operation with a cost of 44 on the entire string, and the string 0100 can be transformed, for example, into 00 by taking the substring 100.

You need to find the minimum cost to transform the entire string ss into the string 1, using several (possibly none) operations, or determine that it is impossible.

∗^{\text{∗}}A binary string is a string that only consists of characters 00 and 11.

†^{\text{†}}A string tt is a substring of a string gg if tt can be obtained from gg by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

给你一个长度为 nn 的二进制字符串∗^{\text{∗}} ss。

对一个长度为 kk 的二进制字符串 gg,一次操作可执行如下步骤:

  • 选择满足 1≤l≤r≤k1 \leq l \leq r \leq k 的下标 l,rl, r;

  • 将字符串 gg 的子串†^{\text{†}} gl,…,grg_l, \ldots, g_r 替换为该子串中出现次数不少于另一字符的某个字符(即:若子串中 0 的个数 ≥\geq 1 的个数,则替换为 0;否则替换为 1)。

该操作的代价为 r−l+1r-l+1。

例如,字符串 010010 可通过一次代价为 44 的操作变为 010:将子串 1001 替换为 1;字符串 1111 可通过对整个字符串执行一次代价为 44 的操作变为 1;字符串 0100 可例如通过选取子串 100 并将其替换为 0 而变为 00。

你需要求出将整个字符串 ss 变为字符串 1 所需的最小总代价(允许执行若干次(包括零次)操作),或判断这是不可能的。

∗^{\text{∗}}二进制字符串是指仅由字符 00 和 11 组成的字符串。

†^{\text{†}}若字符串 tt 可通过从字符串 gg 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到,则称 tt 是 gg 的子串。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5) — the length of the binary string.

The second line of each test case contains a string of length nn, consisting of the characters 0 and 1 — the string ss.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5)—— 二进制字符串的长度。

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

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case, if it is impossible to obtain the string 1, output −1-1. Otherwise, output the minimum cost.

对于每个测试用例,若无法得到字符串 1,则输出 −1-1;否则,输出最小代价。

输入输出样例

  • 输入#1

    7
    1
    0
    1
    1
    2
    00
    2
    10
    3
    010
    8
    00111000
    6
    100100

    输出#1

    -1
    0
    -1
    2
    4
    9
    7

说明/提示

In the first test case, it is impossible to obtain 1, since the only possible operation (l=r=1l = r = 1) does not change the string.

In the second test case, the string is already 1 initially, so the answer is 0.

In the fourth test case, it is possible to perform an operation on the entire string, replacing it with 1. The cost of this operation is 22. It can be shown that it is impossible to obtain the string 1 for a lower cost.

在第一个测试用例中,无法得到字符串 1,因为唯一可能的操作(l=r=1l = r = 1)不会改变原字符串。

在第二个测试用例中,字符串初始即为 1,因此答案为 0。

在第四个测试用例中,可以对整个字符串执行一次操作,将其替换为 1。该操作的代价为 22。可以证明,不存在代价更低的方式得到字符串 1。

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

首页