CF2259F.Binary Bubble Sort Inversions

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Define performing a bubble on an array b1,b2,…,bmb_1, b_2, \ldots, b_m to be performing the following action:

  • For each integer ii (1≤i<m1 \leq i \lt m) in order, if bi>bi+1b_i \gt b_{i+1}, swap bib_i and bi+1b_{i+1}.

Define performing a reverse bubble on an array b1,b2,…,bmb_1, b_2, \ldots, b_m to be performing the following action:

  • For each integer ii (1≤i<m1 \leq i \lt m) in reverse order, if bi>bi+1b_i \gt b_{i+1}, swap bib_i and bi+1b_{i+1}.

For example, if we perform a bubble on [1,1,0][1, 1, 0], we would transform it as follows: [1,1,0]→[1,1,0]→[1,0,1][\color{red}{1, 1}, 0] \to [1, \color{red}{1, 0}] \to [1, 0, 1]. If we perform a reverse bubble on [1,1,0][1, 1, 0], we would transform it as follows: [1,1,0]→[1,0,1]→[0,1,1][1, \color{red}{1, 0}] \to[\color{red}{1, 0}, 1] \to [0, 1, 1].

You are given an array a1,a2,…,ana_1, a_2, \ldots, a_n consisting of only 0s and 1s and a binary string∗^{\text{∗}} ss of length nn. You perform nn operations in order, where the ii-th operation performs a bubble on aa if si=1s_i = \texttt{1} and performs a reverse bubble on aa if si=0s_i = \texttt{0}. For each of the n+1n + 1 states of aa — before any operations have been performed and after each operation — count the number of inversions†^{\text{†}} in aa.

∗^{\text{∗}}A binary string is a string that only contains 0s and 1s.

†^{\text{†}}An inversion in an array cc of length mm is a pair of indices i,ji, j such that 1≤i<j≤m1 \leq i \lt j \leq m and ci>cjc_i \gt c_j.

定义对数组 b1,b2,…,bmb_1, b_2, \ldots, b_m 执行一次**冒泡操作(bubble)**为执行以下动作:

  • 按顺序对每个整数 ii(1≤i<m1 \leq i \lt m),若 bi>bi+1b_i \gt b_{i+1},则交换 bib_i 与 bi+1b_{i+1}。

定义对数组 b1,b2,…,bmb_1, b_2, \ldots, b_m 执行一次**反向冒泡操作(reverse bubble)**为执行以下动作:

  • 按逆序对每个整数 ii(1≤i<m1 \leq i \lt m),若 bi>bi+1b_i \gt b_{i+1},则交换 bib_i 与 bi+1b_{i+1}。

例如,对 [1,1,0][1, 1, 0] 执行一次冒泡操作,其变换过程如下:[1,1,0]→[1,1,0]→[1,0,1][\color{red}{1, 1}, 0] \to [1, \color{red}{1, 0}] \to [1, 0, 1]。而对 [1,1,0][1, 1, 0] 执行一次反向冒泡操作,其变换过程如下:[1,1,0]→[1,0,1]→[0,1,1][1, \color{red}{1, 0}] \to[\color{red}{1, 0}, 1] \to [0, 1, 1]。

给定一个仅由 0 和 1 构成的数组 a1,a2,…,ana_1, a_2, \ldots, a_n,以及一个长度为 nn 的二进制字符串∗^{\text{∗}} ss。你将按顺序执行 nn 次操作:第 ii 次操作中,若 si=1s_i = \texttt{1},则对 aa 执行一次冒泡操作;若 si=0s_i = \texttt{0},则对 aa 执行一次反向冒泡操作。对于 aa 的 n+1n + 1 个状态——即初始状态(尚未执行任何操作)以及每次操作之后的状态——请分别统计此时 aa 中的逆序对数†^{\text{†}}。

∗^{\text{∗}}二进制字符串是指仅包含字符 0 和 1 的字符串。

†^{\text{†}}数组 cc(长度为 mm)中的一个逆序对是指一对下标 (i,j)(i, j),满足 1≤i<j≤m1 \leq i \lt j \leq m 且 ci>cjc_i \gt c_j。

输入格式

The first line of each input contains tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

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

The second line of each test case contains a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤10 \leq a_i \leq 1) — the array aa.

The third line of each test case contains ss (si∈0,1s_i \in {0, 1}, ∣s∣=n|s| = n∗^{\text{∗}}) — the binary string ss.

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

∗^{\text{∗}}∣c∣|c| denotes the length of a string cc.

每组输入的第一行包含 tt(1≤t≤1041 \leq t \leq 10^4)—— 测试用例的数量。

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

每个测试用例的第二行包含 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤10 \leq a_i \leq 1)—— 数组 aa。

每个测试用例的第三行包含字符串 ss(si∈{0,1}s_i \in \{0, 1\},∣s∣=n|s| = n∗^{\text{∗}})—— 二进制字符串 ss。

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

∗^{\text{∗}}∣c∣|c| 表示字符串 cc 的长度。

输出格式

For each test case, output n+1n + 1 space separated integers, the ii-th integer denoting the number of inversions in aa after the first i−1i - 1 operations have been performed.

对于每个测试用例,输出 n+1n + 1 个以空格分隔的整数,其中第 ii 个整数表示对数组 aa 执行前 i−1i - 1 次操作后的逆序对数量。

输入输出样例

  • 输入#1

    7
    4
    1 1 0 0
    1010
    7
    0 1 0 1 1 0 0
    0101001
    4
    0 0 1 1
    1001
    1
    1
    1
    5
    0 1 1 0 0
    11101
    3
    1 0 0
    000
    6
    1 0 1 1 0 0
    011000

    输出#1

    4 2 1 0 0 
    7 4 2 0 0 0 0 0 
    0 0 0 0 0 
    0 0 
    4 2 0 0 0 0 
    2 1 0 0 
    7 4 2 1 0 0 0

说明/提示

In the first test case:

  • Before any operations: a=[1,1,0,0]a = [1, 1, 0, 0], which has 44 inversions.
  • After the first operation: a=[1,0,0,1]a = [1, 0, 0, 1], which has 22 inversions.
  • After the second operation: a=[0,1,0,1]a = [0, 1, 0, 1], which has 11 inversion.
  • After the third operation: a=[0,0,1,1]a = [0, 0, 1, 1], which has 00 inversions.
  • After the fourth operation: a=[0,0,1,1]a = [0, 0, 1, 1], which has 00 inversions.

在第一个测试用例中:

  • 任何操作之前:a=[1,1,0,0]a = [1, 1, 0, 0],其逆序对数量为 44。
  • 第一次操作后:a=[1,0,0,1]a = [1, 0, 0, 1],其逆序对数量为 22。
  • 第二次操作后:a=[0,1,0,1]a = [0, 1, 0, 1],其逆序对数量为 11。
  • 第三次操作后:a=[0,0,1,1]a = [0, 0, 1, 1],其逆序对数量为 00。
  • 第四次操作后:a=[0,0,1,1]a = [0, 0, 1, 1],其逆序对数量为 00。

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

首页