No.2145 Segment +-
タグ : / 解いたユーザー数 28
作問者 :
milkcoffee
/ テスター :
👑
hamamu
nok0
Problem Statement
You are given a string $S$ of length $N$ consisting of +,-,?. You will create an integer sequence $A$ of length $N$ according to the following rules.
Here, $S_i$ denotes the $i$-th character of the string $S$, and $A_i$ denotes the $i$-th element of the integer sequence $A$.
- If $S_i=$
+, set $A_i=1$. - If $S_i=$
-, set $A_i=-1$. - If $S_i=$
?, set $A_i=1$ or $A_i=-1$.
For the created $A$, let $f(A)$ be the answer to the following problem.
There is an integer sequence $A$ of length $N$, and you can perform the following operation on $A$.
- Choose integers $l,r \ (1 \leq l \leq r \leq N)$ and multiply each of the values $A_l, A_{l+1}, \cdots , A_r$ by $-1$.
Find the minimum number of operations required to achieve the following condition.
- Condition: Let $B_i= \sum_{j=1}^{i} A_j$. For all $i$, $B_i \geq 0$ holds.
Find the minimum value of $f(A)$.
Input
$N$ $S$
- $1 \leq N \leq 2 \times 10^5$
- $S$ is a string of length $N$ consisting of
+,-,? - $N$ is an integer.
Output
Print the minimum value of $f(A)$ as an integer.
Sample
Sample 1
Input
5 +--?-
Output
1
There are $2$ possibilities for $A$: $(1,-1,-1,1,-1), (1,-1,-1,-1,-1)$.
For example, when $A=(1,-1,-1,1,-1)$, we have $B=(1,0,-1,0,-1)$.
Performing the operation with $l=2, r=3$ gives $A=(1,1,1,1,-1)$ $B=(1,2,3,4,3)$.
The condition can be achieved with $1$ operation, so $f(A)=1$ in this case.
In the case $A=(1,-1,-1,-1,-1)$ as well, $f(A)=1$. Therefore, the minimum value of $f(A)$ is $1$.
Sample 2
Input
4 ??--
Output
0
Only when $A=(1,1,-1,-1)$ does $f(A)=0$ hold.
Sample 3
Input
13 -?+++---?----
Output
2
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。