311310: CF1969B. Shifts and Sorting

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Shifts and Sortingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Let's define a cyclic shift of some string $s$ as a transformation from $s_1 s_2 \dots s_{n-1} s_{n}$ into $s_{n} s_1 s_2 \dots s_{n-1}$. In other words, you take one last character $s_n$ and place it before the first character while moving all other characters to the right.

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

In one operation, you can choose any substring $s_l s_{l+1} \dots s_r$ ($1 \le l < r \le |s|$) and cyclically shift it. The cost of such operation is equal to $r - l + 1$ (or the length of the chosen substring).

You can perform the given operation any number of times. What is the minimum total cost to make $s$ sorted in non-descending order?

Input

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first and only line of each test case contains a binary string $s$ ($2 \le |s| \le 2 \cdot 10^5$; $s_i \in$ {0, 1}) — the string you need to sort.

Additional constraint on the input: the sum of lengths of strings over all test cases doesn't exceed $2 \cdot 10^5$.

Output

For each test case, print the single integer — the minimum total cost to make string sorted using operation above any number of times.

ExampleInput
5
10
0000
11000
101011
01101001
Output
2
0
9
5
11
Note

In the first test case, you can choose the whole string and perform a cyclic shift: 10 $\rightarrow$ 01. The length of the substring is $2$, so the cost is $2$.

In the second test case, the string is already sorted, so you don't need to perform any operations.

In the third test case, one of the optimal strategies is the next:

  1. choose substring $[1, 3]$: 11000 $\rightarrow$ 01100;
  2. choose substring $[2, 4]$: 01100 $\rightarrow$ 00110;
  3. choose substring $[3, 5]$: 00110 $\rightarrow$ 00011.
The total cost is $3 + 3 + 3 = 9$.

Output

题目大意:
给定一个由0和1组成的二进制字符串s。你可以选择字符串的任意一个子串s_l s_{l+1} ... s_r(1≤l
输入数据格式:
第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。
每个测试用例的第一行包含一个二进制字符串s(2≤|s|≤2*10^5;s_i∈{0, 1})——需要排序的字符串。
所有测试用例的字符串长度之和不超过2*10^5。

输出数据格式:
对于每个测试用例,输出使其排序的最小总成本。

示例:
输入:
5
10
0000
11000
101011
01101001

输出:
2
0
9
5
11

注意:
在第一个测试用例中,你可以选择整个字符串并执行循环移位:10 → 01。子串的长度为2,因此成本为2。
在第二个测试用例中,字符串已经排序,不需要执行任何操作。
在第三个测试用例中,一个最优策略是:
1. 选择子串[1, 3]:11000 → 01100;
2. 选择子串[2, 4]:01100 → 00110;
3. 选择子串[3, 5]:00110 → 00011。
总成本为3 + 3 + 3 = 9。题目大意: 给定一个由0和1组成的二进制字符串s。你可以选择字符串的任意一个子串s_l s_{l+1} ... s_r(1≤l

加入题单

上一题 下一题 算法标签: