CF2259F.Binary Bubble Sort Inversions
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Define performing a bubble on an array b1,b2,…,bm to be performing the following action:
- For each integer i (1≤i<m) in order, if bi>bi+1, swap bi and bi+1.
Define performing a reverse bubble on an array b1,b2,…,bm to be performing the following action:
- For each integer i (1≤i<m) in reverse order, if bi>bi+1, swap bi and bi+1.
For example, if we perform a bubble on [1,1,0], we would transform it as follows: [1,1,0]→[1,1,0]→[1,0,1]. If we perform a reverse bubble on [1,1,0], we would transform it as follows: [1,1,0]→[1,0,1]→[0,1,1].
You are given an array a1,a2,…,an consisting of only 0s and 1s and a binary string∗ s of length n. You perform n operations in order, where the i-th operation performs a bubble on a if si=1 and performs a reverse bubble on a if si=0. For each of the n+1 states of a — before any operations have been performed and after each operation — count the number of inversions† in a.
∗A binary string is a string that only contains 0s and 1s.
†An inversion in an array c of length m is a pair of indices i,j such that 1≤i<j≤m and ci>cj.
定义对数组 b1,b2,…,bm 执行一次**冒泡操作(bubble)**为执行以下动作:
- 按顺序对每个整数 i(1≤i<m),若 bi>bi+1,则交换 bi 与 bi+1。
定义对数组 b1,b2,…,bm 执行一次**反向冒泡操作(reverse bubble)**为执行以下动作:
- 按逆序对每个整数 i(1≤i<m),若 bi>bi+1,则交换 bi 与 bi+1。
例如,对 [1,1,0] 执行一次冒泡操作,其变换过程如下:[1,1,0]→[1,1,0]→[1,0,1]。而对 [1,1,0] 执行一次反向冒泡操作,其变换过程如下:[1,1,0]→[1,0,1]→[0,1,1]。
给定一个仅由 0 和 1 构成的数组 a1,a2,…,an,以及一个长度为 n 的二进制字符串∗ s。你将按顺序执行 n 次操作:第 i 次操作中,若 si=1,则对 a 执行一次冒泡操作;若 si=0,则对 a 执行一次反向冒泡操作。对于 a 的 n+1 个状态——即初始状态(尚未执行任何操作)以及每次操作之后的状态——请分别统计此时 a 中的逆序对数†。
∗二进制字符串是指仅包含字符 0 和 1 的字符串。
†数组 c(长度为 m)中的一个逆序对是指一对下标 (i,j),满足 1≤i<j≤m 且 ci>cj。
输入格式
The first line of each input contains t (1≤t≤104) — the number of test cases.
The first line of each test case contains n (1≤n≤5⋅105) — the length of a and s.
The second line of each test case contains a1,a2,…,an (0≤ai≤1) — the array a.
The third line of each test case contains s (si∈0,1, ∣s∣=n∗) — the binary string s.
It is guaranteed that the sum of n over all test cases does not exceed 5⋅105.
∗∣c∣ denotes the length of a string c.
每组输入的第一行包含 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含 n(1≤n≤5⋅105)—— 数组 a 和字符串 s 的长度。
每个测试用例的第二行包含 a1,a2,…,an(0≤ai≤1)—— 数组 a。
每个测试用例的第三行包含字符串 s(si∈{0,1},∣s∣=n∗)—— 二进制字符串 s。
保证所有测试用例的 n 之和不超过 5⋅105。
∗∣c∣ 表示字符串 c 的长度。
输出格式
For each test case, output n+1 space separated integers, the i-th integer denoting the number of inversions in a after the first i−1 operations have been performed.
对于每个测试用例,输出 n+1 个以空格分隔的整数,其中第 i 个整数表示对数组 a 执行前 i−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], which has 4 inversions.
- After the first operation: a=[1,0,0,1], which has 2 inversions.
- After the second operation: a=[0,1,0,1], which has 1 inversion.
- After the third operation: a=[0,0,1,1], which has 0 inversions.
- After the fourth operation: a=[0,0,1,1], which has 0 inversions.
在第一个测试用例中:
- 任何操作之前:a=[1,1,0,0],其逆序对数量为 4。
- 第一次操作后:a=[1,0,0,1],其逆序对数量为 2。
- 第二次操作后:a=[0,1,0,1],其逆序对数量为 1。
- 第三次操作后:a=[0,0,1,1],其逆序对数量为 0。
- 第四次操作后:a=[0,0,1,1],其逆序对数量为 0。
输入解题思路,AI测评打分。不知道怎么写?