Spaces:
Sleeping
Sleeping
This is the easy version of the problem. The only difference between the two versions are the allowed characters in s. In the easy version, s only contains the character ?. You can make hacks only if both versions of the problem are solved.You are given a permutation p of length n. You are also given a string s of length n, consisting only of the character ?.For each i from 1 to n: Define li as the largest index j<i such that pj>pi. If there is no such index, li:=i. Define ri as the smallest index j>i such that pj>pi. If there is no such index, ri:=i. Initially, you have an undirected graph with n vertices (numbered from 1 to n) and no edges. Then, for each i from 1 to n, add one edge to the graph: If si= L, add the edge (i,li) to the graph. If si= R, add the edge (i,ri) to the graph. If si= ?, either add the edge (i,li) or the edge (i,ri) to the graph at your choice. Find the maximum possible diameter∗ over all connected graphs that you can form. Output −1 if it is not possible to form any connected graphs.∗ Let d(s,t) denote the smallest number of edges on any path between s and t.The diameter of the graph is defined as the maximum value of d(s,t) over all pairs of vertices s and t. |