#C10751. Coin Arrangement Challenge
Coin Arrangement Challenge
Coin Arrangement Challenge
In this problem, you are given ( n ) coins each with an associated integer value and a set of ( m ) restricted pairs. Your task is to determine if it is possible to arrange the coins in a sequence where the coin values strictly increase from left to right and no two coins that are restricted appear consecutively. The restrictions are given as pairs ( (a, b) ) (with 1-indexed coin labels) indicating that coin ( a ) and coin ( b ) cannot be adjacent in the final arrangement.
Formally, let the sorted order of coin values be ( v_1, v_2, \ldots, v_n ) and let the corresponding coin indices be ( i_1, i_2, \ldots, i_n ) after sorting by value. The arrangement is valid if for all ( j = 1, 2, \ldots, n-1 ), the pair ( (i_j, i_{j+1}) ) is not among the restricted pairs. If a valid sequence exists, output "YES" and the sequence of sorted coin values; otherwise output "NO".
inputFormat
Input is given via standard input (stdin) in the following format:
- The first line contains a single integer ( n ) ( (1 \leq n \leq 10^5) ) representing the number of coins.
- The second line contains ( n ) space-separated integers ( v_1, v_2, \ldots, v_n ) representing the values of the coins.
- The third line contains a single integer ( m ) ( (0 \leq m \leq 10^5) ) representing the number of restricted pairs.
- The next ( m ) lines each contain two space-separated integers ( a ) and ( b ) ( (1 \leq a, b \leq n, a \neq b) ) denoting that coins ( a ) and ( b ) cannot be placed adjacent to each other.
Note: All coins are considered 1-indexed based on their input order.
outputFormat
Output to standard output (stdout) in the following format:
- If a valid arrangement exists, print "YES" on the first line, followed by the sorted coin values (in increasing order) separated by spaces on the second line.
- If no valid arrangement exists, print just "NO" on a single line.## sample
5
4 2 5 1 3
2
1 2
4 5
YES
1 2 3 4 5
</p>