Solution
Code
B. Thousand Sunny's Network Setup
Solution
Code
C. Robin’s Water Wisdom: Stop the Leaks!
Solution
Code
D. Pirates Island: Painting the Grand Line
Solution
Code
Solution
Code
for _ in range(t): n = int(input()) chest_values = list(map(int, input().split())) chest_colors = input()
blue_chests = []
red_chests = []
for i in range(n):
if chest_colors[i] == "B":
blue_chests.append(chest_values[i])
else:
red_chests.append(chest_values[i])
blue_chests.sort()
red_chests.sort()
is_permutation_possible = True
for i in range(len(blue_chests)):
if blue_chests[i] < i + 1:
is_permutation_possible = False
for i in range(len(red_chests)):
if red_chests[i] > (n - len(red_chests) + i + 1):
is_permutation_possible = False
print("YES" if is_permutation_possible else "NO") </spoiler>
[F. Luffy’s Lineup Challenge](https://codeforces.me/gym/594356/problem/F)
<spoiler summary="Solution">
The problem involves rearranging an array b to match the order of another array a using adjacent swaps. The key insight is that both arrays are guaranteed to be permutations of the same set of elements (i.e., they are multisets), meaning that we can always reorder b to match a. We start by creating a mapping of each value in a to its corresponding index, allowing us to track where each value from b should be placed in a. For each element in b, we replace it with the index from a, resulting in a list of target positions.Once we have this list of target positions, we simulate sorting it into the correct order using adjacent swaps (similar to bubble sort). At each step, we compare adjacent elements, and if they are in the wrong order, we swap them. We continue this process until the list is sorted, recording each swap. Finally, we output the number of swaps and the swap operations themselves. This approach guarantees the desired configuration while ensuring the solution is efficient enough given the problem constraints.
</spoiler>
<spoiler summary= "Code">
n = int(input()) a = list(map(int,input().split())) b = list(map(int, input().split())) def next_index(start,target): for i in range(start,n): if b[i] == target: return i swaps = [] for i in range(n): target = a[i] right = next_index(i,target)
while right>i:
swaps.append([right,right+1])
b[right],b[right-1] = b[right-1],b[right]
right-=1count = 0 print(len(swaps)) for swap in swaps: print(*swap) ```



