310997: CF1919B. Plus-Minus Split

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

Description

B. Plus-Minus Splittime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a string $s$ of length $n$ consisting of characters "+" and "-". $s$ represents an array $a$ of length $n$ defined by $a_i=1$ if $s_i=$ "+" and $a_i=-1$ if $s_i=$ "-".

You will do the following process to calculate your penalty:

  1. Split $a$ into non-empty arrays $b_1,b_2,\ldots,b_k$ such that $b_1+b_2+\ldots+b_k=a^\dagger$, where $+$ denotes array concatenation.
  2. The penalty of a single array is the absolute value of its sum multiplied by its length. In other words, for some array $c$ of length $m$, its penalty is calculated as $p(c)=|c_1+c_2+\ldots+c_m| \cdot m$.
  3. The total penalty that you will receive is $p(b_1)+p(b_2)+\ldots+p(b_k)$.

If you perform the above process optimally, find the minimum possible penalty you will receive.

$^\dagger$ Some valid ways to split $a=[3,1,4,1,5]$ into $(b_1,b_2,\ldots,b_k)$ are $([3],[1],[4],[1],[5])$, $([3,1],[4,1,5])$ and $([3,1,4,1,5])$ while some invalid ways to split $a$ are $([3,1],[1,5])$, $([3],[\,],[1,4],[1,5])$ and $([3,4],[5,1,1])$.

Input

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) — the length of string $s$.

The second line of each test case contains string $s$ ($s_i \in \{ \mathtt{+}, \mathtt{-} \}$, $|s| = n$).

Note that there are no constraints on the sum of $n$ over all test cases.

Output

For each test case, output a single integer representing the minimum possible penalty you will receive.

ExampleInput
5
1
+
5
-----
6
+-+-+-
10
--+++++++-
20
+---++++-+++++---++-
Output
1
5
0
4
4
Note

In the first test case, we have $a=[1]$. We can split array $a$ into $([1])$. Then, the sum of penalties of the subarrays is $p([1]) = 1$.

In the second test case, we have $a=[-1,-1,-1,-1,-1]$. We can split array $a$ into $([-1],[-1],[-1],[-1],[-1])$. Then, the sum of penalties of the subarrays is $p([-1]) + p([-1]) + p([-1]) + p([-1]) + p([-1]) = 1 + 1 + 1 + 1 + 1 = 5$.

In the third test case, we have $a=[1,-1,1,-1,1,-1]$. We can split array $a$ into $([1,-1,1,-1],[1,-1])$. Then, the sum of penalties of the subarrays is $p([1,-1,1,-1]) + p([1,-1]) = 0 + 0 = 0$.

Output

题目大意:
这个题目是关于如何将一个由"+"和"-"组成的字符串s最优地分割成若干非空子串,以便最小化惩罚值的问题。字符串s的长度为n,每个"+"代表1,每个"-"代表-1,因此s定义了一个数组a。惩罚值的计算方式是将a分割成k个非空子数组b1, b2, ..., bk,每个子数组的惩罚值是其元素和的绝对值乘以其长度,总惩罚值是所有子数组惩罚值的和。需要计算的是最小可能的总惩罚值。

输入输出数据格式:
输入:
- 第一行是一个整数t(1 ≤ t ≤ 1000),表示测试用例的数量。
- 每个测试用例有两行,第一行是一个整数n(1 ≤ n ≤ 5000),表示字符串s的长度。
- 第二行是字符串s,s只包含字符"+"和"-",且长度为n。

输出:
- 对于每个测试用例,输出一行,包含一个整数,表示最小可能的总惩罚值。

示例:
输入:
```
5
1
+
5
-----
6
+-+-+-
10
--+++++++-
20
+---++++-+++++---++
```
输出:
```
1
5
0
4
4
```

注意:
- 在计算子数组的惩罚值时,需要计算数组元素和的绝对值乘以数组长度。
- 测试用例中n的总和没有限制。题目大意: 这个题目是关于如何将一个由"+"和"-"组成的字符串s最优地分割成若干非空子串,以便最小化惩罚值的问题。字符串s的长度为n,每个"+"代表1,每个"-"代表-1,因此s定义了一个数组a。惩罚值的计算方式是将a分割成k个非空子数组b1, b2, ..., bk,每个子数组的惩罚值是其元素和的绝对值乘以其长度,总惩罚值是所有子数组惩罚值的和。需要计算的是最小可能的总惩罚值。 输入输出数据格式: 输入: - 第一行是一个整数t(1 ≤ t ≤ 1000),表示测试用例的数量。 - 每个测试用例有两行,第一行是一个整数n(1 ≤ n ≤ 5000),表示字符串s的长度。 - 第二行是字符串s,s只包含字符"+"和"-",且长度为n。 输出: - 对于每个测试用例,输出一行,包含一个整数,表示最小可能的总惩罚值。 示例: 输入: ``` 5 1 + 5 ----- 6 +-+-+- 10 --+++++++- 20 +---++++-+++++---++ ``` 输出: ``` 1 5 0 4 4 ``` 注意: - 在计算子数组的惩罚值时,需要计算数组元素和的绝对值乘以数组长度。 - 测试用例中n的总和没有限制。

加入题单

上一题 下一题 算法标签: