CF1919B.Plus-Minus Split

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a string ss of length nn consisting of characters "+" and "-". ss represents an array aa of length nn defined by ai=1a_i=1 if si=s_i= "+" and ai=−1a_i=-1 if si=s_i= "-".

You will do the following process to calculate your penalty:

  1. Split aa into non-empty arrays b1,b2,…,bkb_1,b_2,\ldots,b_k such that b1+b2+…+bk=a†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 cc of length mm, its penalty is calculated as p(c)=∣c1+c2+…+cm∣⋅mp(c)=|c_1+c_2+\ldots+c_m| \cdot m.
  3. The total penalty that you will receive is p(b1)+p(b2)+…+p(bk)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]a=[3,1,4,1,5] into (b1,b2,…,bk)(b_1,b_2,\ldots,b_k) are ([3],[1],[4],[1],[5])([3],[1],[4],[1],[5]), ([3,1],[4,1,5])([3,1],[4,1,5]) and ([3,1,4,1,5])([3,1,4,1,5]) while some invalid ways to split aa are ([3,1],[1,5])([3,1],[1,5]), ([3],[ ],[1,4],[1,5])([3],[\,],[1,4],[1,5]) and ([3,4],[5,1,1])([3,4],[5,1,1]).

给你一个长度为 nn 的字符串 ss,它仅由字符 "+" 和 "-" 组成。该字符串表示一个长度为 nn 的数组 aa,其定义为:若 si=s_i = "+",则 ai=1a_i = 1;若 si=s_i = "-",则 ai=−1a_i = -1。

你将执行如下过程来计算你的罚分(penalty):

  1. 将数组 aa 划分为若干个非空子数组 b1,b2,…,bkb_1, b_2, \ldots, b_k,使得 b1+b2+…+bk=a†b_1 + b_2 + \ldots + b_k = a^\dagger,其中 ++ 表示数组的拼接(concatenation)。
  2. 单个子数组的罚分为其元素和的绝对值乘以其长度。换言之,对某个长度为 mm 的数组 cc,其罚分定义为 p(c)=∣c1+c2+…+cm∣⋅mp(c) = |c_1 + c_2 + \ldots + c_m| \cdot m。
  3. 你最终获得的总罚分为 p(b1)+p(b2)+…+p(bk)p(b_1) + p(b_2) + \ldots + p(b_k)。

若你以最优方式执行上述过程,求你能获得的最小可能罚分。

†^\dagger 对于 a=[3,1,4,1,5]a = [3, 1, 4, 1, 5],一些合法的划分方式 (b1,b2,…,bk)(b_1, b_2, \ldots, b_k) 包括:([3],[1],[4],[1],[5])([3], [1], [4], [1], [5])、([3,1],[4,1,5])([3,1], [4,1,5]) 和 ([3,1,4,1,5])([3,1,4,1,5]);而一些非法的划分方式包括:([3,1],[1,5])([3,1], [1,5])、([3],[ ],[1,4],[1,5])([3], [\,], [1,4], [1,5]) 以及 ([3,4],[5,1,1])([3,4], [5,1,1])。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤10001 \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 nn (1≤n≤50001 \le n \le 5000) — the length of string ss.

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

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

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

每个测试用例的第一行包含一个整数 nn(1≤n≤50001 \le n \le 5000),表示字符串 ss 的长度。

每个测试用例的第二行包含字符串 ss(其中 si∈+,−s_i \in { \mathtt{+}, \mathtt{-} },且 ∣s∣=n|s| = n)。

注意:所有测试用例的 nn 之和没有额外限制。

输出格式

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

对于每个测试用例,输出一个整数,表示你将获得的最小可能罚分。

输入输出样例

  • 输入#1

    5
    1
    +
    5
    -----
    6
    +-+-+-
    10
    --+++++++-
    20
    +---++++-+++++---++-

    输出#1

    1
    5
    0
    4
    4

说明/提示

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

In the second test case, we have a=[−1,−1,−1,−1,−1]a=[-1,-1,-1,-1,-1]. We can split array aa into ([−1],[−1],[−1],[−1],[−1])([-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=5p([-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]a=[1,-1,1,-1,1,-1]. We can split array aa into ([1,−1,1,−1],[1,−1])([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=0p([1,-1,1,-1]) + p([1,-1]) = 0 + 0 = 0.

在第一个测试用例中,我们有 a=[1]a=[1]。我们可以将数组 aa 分割为 ([1])([1])。此时,各子数组的惩罚值之和为 p([1])=1p([1]) = 1。

在第二个测试用例中,我们有 a=[−1,−1,−1,−1,−1]a=[-1,-1,-1,-1,-1]。我们可以将数组 aa 分割为 ([−1],[−1],[−1],[−1],[−1])([-1],[-1],[-1],[-1],[-1])。此时,各子数组的惩罚值之和为 p([−1])+p([−1])+p([−1])+p([−1])+p([−1])=1+1+1+1+1=5p([-1]) + p([-1]) + p([-1]) + p([-1]) + p([-1]) = 1 + 1 + 1 + 1 + 1 = 5。

在第三个测试用例中,我们有 a=[1,−1,1,−1,1,−1]a=[1,-1,1,-1,1,-1]。我们可以将数组 aa 分割为 ([1,−1,1,−1],[1,−1])([1,-1,1,-1],[1,-1])。此时,各子数组的惩罚值之和为 p([1,−1,1,−1])+p([1,−1])=0+0=0p([1,-1,1,-1]) + p([1,-1]) = 0 + 0 = 0。

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

首页