#P1987G1. Spinning Round (Easy Version)
Spinning Round (Easy Version)
No submission language available for this problem.
Description
This is the easy version of the problem. The only difference between the two versions are the allowed characters in . In the easy version, only contains the character ?. You can make hacks only if both versions of the problem are solved.
You are given a permutation of length . You are also given a string of length , consisting only of the character ?.
For each from to :
- Define as the largest index such that . If there is no such index, .
- Define as the smallest index such that . If there is no such index, .
Initially, you have an undirected graph with vertices (numbered from to ) and no edges. Then, for each from to , add one edge to the graph:
- If L, add the edge to the graph.
- If R, add the edge to the graph.
- If ?, either add the edge or the edge to the graph at your choice.
Find the maximum possible diameter over all connected graphs that you can form. Output if it is not possible to form any connected graphs.
Let denote the smallest number of edges on any path between and .
The diameter of the graph is defined as the maximum value of over all pairs of vertices and .
Each test contains multiple test cases. The first line of input contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer () — the length of the permutation .
The second line of each test case contains integers () — the elements of , which are guaranteed to form a permutation.
The third line of each test case contains a string of length . It is guaranteed that it consists only of the character ?.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output the maximum possible diameter over all connected graphs that you form, or if it is not possible to form any connected graphs.
Input
Each test contains multiple test cases. The first line of input contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer () — the length of the permutation .
The second line of each test case contains integers () — the elements of , which are guaranteed to form a permutation.
The third line of each test case contains a string of length . It is guaranteed that it consists only of the character ?.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the maximum possible diameter over all connected graphs that you form, or if it is not possible to form any connected graphs.
Note
In the first test case, here are some possible connected graphs that you can form (the labels are indices):
![]() | ![]() | ![]() |
In the second test case, the only connected graph has a diameter of .