CF1750H.BinaryStringForces

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a binary string ss of length nn. We define a maximal substring as a substring that cannot be extended while keeping all elements equal. For example, in the string 1100011111000111 there are three maximal substrings: 1111, 000000 and 111111.

In one operation, you can select two maximal adjacent substrings. Since they are maximal and adjacent, it's easy to see their elements must have different values. Let aa be the length of the sequence of ones and bb be the length of the sequence of zeros. Then do the following:

  • If a≥ba \ge b, then replace bb selected zeros with bb ones.
  • If a<ba \lt b, then replace aa selected ones with aa zeros.

As an example, for 11100001110000 we make it 00000000000000, for 00110011 we make it 11111111. We call a string being good if it can be turned into 1111...11111111...1111 using the aforementioned operation any number of times (possibly, zero). Find the number of good substrings among all n(n+1)2\frac{n(n+1)}{2} non-empty substrings of ss.

给你一个长度为 nn 的二进制字符串 ss。我们定义极大子串为:无法在保持所有字符相等的前提下进一步向左右扩展的子串。例如,在字符串 1100011111000111 中,存在三个极大子串:1111、000000 和 111111。

一次操作中,你可以选择两个相邻的极大子串。由于它们是极大且相邻的,显然它们所含字符必定不同。设 aa 为其中全 11 子串的长度,bb 为其中全 00 子串的长度。然后执行以下操作:

  • 若 a≥ba \ge b,则将选中的 bb 个 00 全部替换为 bb 个 11;
  • 若 a<ba \lt b,则将选中的 aa 个 11 全部替换为 aa 个 00。

例如,对 11100001110000 执行该操作后得到 00000000000000;对 00110011 执行该操作后得到 11111111。若一个字符串可通过上述操作(可执行任意多次,包括零次)变为全 11 字符串 1111…11111111\ldots1111,则称其为好字符串。求 ss 的所有 n(n+1)2\frac{n(n+1)}{2} 个非空子串中,好字符串的个数。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The description of test cases follows.

The first line of each test case contains nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the string ss.

The second line of each test case contains the binary string ss of length nn.

It is guaranteed that sum of nn across all test cases doesn't exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。

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

输出格式

For each test case, print a single integer — the number of good substrings.

对于每个测试用例,输出一个整数——即“好”子串的数量。

输入输出样例

  • 输入#1

    4
    6
    100011
    3
    101
    5
    11111
    6
    010101

    输出#1

    8
    5
    15
    18

说明/提示

Let's define a substring from index ll to index rr as [l,r][l, r].

For the first test case, the good substrings are:

  • [1,1][1,1],
  • [1,2][1,2],
  • [3,6][3,6],
  • [4,5][4,5],
  • [4,6][4,6],
  • [5,5][5,5],
  • [5,6][5,6],
  • [6,6][6,6].

In the second test case, all substrings are good except [2,2][2,2].

In the third test case, all substrings are good.

我们定义从索引 ll 到索引 rr 的子串为 [l,r][l, r]。

对于第一个测试用例,所有“好”的子串为:

  • [1,1][1,1],
  • [1,2][1,2],
  • [3,6][3,6],
  • [4,5][4,5],
  • [4,6][4,6],
  • [5,5][5,5],
  • [5,6][5,6],
  • [6,6][6,6]。

在第二个测试用例中,除 [2,2][2,2] 外,所有子串都是“好”的。

在第三个测试用例中,所有子串都是“好”的。

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

首页