問題一覧 > 通常問題

No.3061 Cut and Maximums

レベル : / AI推定難易度 : 2000 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 14
作問者 : 👑 tute7627 / テスター : 👑 rin204 SPD_9X2
お気に入りにしたユーザー ProblemId : 11171 / 自分の提出
問題文最終更新日: 2025-03-12 02:46:31
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 460 (順位表) の他の問題:

Problem Statement

There is an undirected graph $G$ with $N$ vertices and $N$ edges.

The edges of $G$ are given by a permutation $P$ of $(1,2,\dots,N)$. For $1 \le i \le N$, the $i$-th edge connects vertex $P_i$ and vertex $P_{i+1}$. Here, vertex $P_{N+1}$ refers to vertex $P_1$.

Initially, all vertices of the graph $G$ are painted white.

You may perform the following sequence of operations any number of times, $0$ or more.

  1. Choose $0$ or more edges of the graph $G$ and remove them.
  2. For each connected component of the graph $G$, perform the following process.
    • Paint black the vertex with the largest vertex number in the connected component. If that vertex is already painted black, do nothing.
  3. Restore the removed edges of the graph $G$.

You are given a string $S$ of length $N$ consisting of B and W. You want vertex $i$ to be painted black if the $i$-th character of $S$ is B, and vertex $i$ to be painted white otherwise.

Determine whether this can be achieved by performing the operations, and if it is possible, find the minimum number of operations required.

Input

$N$
$P_1\ P_2\ \dots \ P_N$
$S$

  • $2 \le N \le 3 \times 10^5$
  • $P$ is a permutation of $(1,2,\dots,N)$.
  • $S$ is a string of length $N$ consisting of B and W.

Output

If the goal can be achieved, print the minimum number of operations; otherwise, print -1. Print a newline at the end.

Sample

Sample 1
Input
4
2 4 1 3
BBWB
Output
2

The goal can be achieved by the following steps.

  1. Remove edge $(2,4)$ and edge $(3,2)$. Since vertex $2$ and vertices $1,3,4$ each form a connected component, paint vertex $2$ and vertex $4$ black.
  2. Remove edge $(4,1)$ and edge $(1,3)$. Since vertex $1$ and vertices $2,3,4$ each form a connected component, paint vertex $1$ black.

Sample 2
Input
4
2 4 1 3
BBBB
Output
1

It suffices to remove all the edges.

Sample 3
Input
4
2 4 1 3
BBBW
Output
-1

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。