#C1804. Sort Sequence by Swapping Specified Elements

    ID: 45050 Type: Default 1000ms 256MiB

Sort Sequence by Swapping Specified Elements

Sort Sequence by Swapping Specified Elements

You are given an integer n, a sequence of n integers \(a_1, a_2, \dots, a_n\), and two special integers \(X\) and \(Y\). You are allowed to swap only the elements that are exactly equal to \(X\) or \(Y\). In other words, you can swap any occurrence of \(X\) with any occurrence of \(Y\). Your task is to determine whether it is possible to sort the sequence in non-decreasing order by using only such swaps.

Note: If an element is out of place in the sorted order and is not equal to either \(X\) or \(Y\), then it cannot be swapped, and the sequence cannot be sorted using the allowed operation.

More formally, let \(S = [a_1, a_2, \dots, a_n]\) and let \(S_{sorted}\) be the sorted version of \(S\) in non-decreasing order. For every index \(i\) where \(a_i \neq S_{sorted}[i]\), it must be the case that \(a_i = X\) or \(a_i = Y\) in order for the swap operation to potentially sort the sequence.

inputFormat

The input consists of three lines:

  1. The first line contains an integer (n) representing the length of the sequence.
  2. The second line contains (n) space-separated integers, which represent the sequence (a_1, a_2, \dots, a_n).
  3. The third line contains two space-separated integers, (X) and (Y), indicating the values that can be swapped.

outputFormat

Output a single line containing either "YES" if the sequence can be sorted using only swaps between elements equal to (X) and (Y), or "NO" if it cannot.## sample

5
1 3 2 3 2
2 3
YES

</p>