Shoe Shuffling (Codeforces 1691B)
Tags: Greedy, two pointers

A class of students got bored wearing the same pair of shoes every day, so they decided to shuffle their shoes among themselves. In this problem, a pair of shoes is inseparable and is considered as a single object.There are $$$n$$$ students in the class, and you are given an array $$$s$$$ in non-decreasing order, where $$$s_i$$$ is the shoe size of the $$$i$$$-th student. A shuffling of shoes is valid only if no student gets their own shoes and if every student gets shoes of size greater than or equal to their size. You have to output a permutation $$$p$$$ of $$$\{1,2,\ldots,n\}$$$ denoting a valid shuffling of shoes, where the $$$i$$$-th student gets the shoes of the $$$p_i$$$-th student ($$$p_i \ne i$$$). And output $$$-1$$$ if a valid shuffling does not exist.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

Input: Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1\leq n\leq10^5$$$) — the number of students.The second line of each test case contains $$$n$$$ integers $$$s_1, s_2,\ldots,s_n$$$ ($$$1\leq s_i\leq10^9$$$, and for all $$$1\le i<n$$$, $$$s_i\le s_{i+1}$$$) — the shoe sizes of the students.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case, print the answer in a single line using the following format.If a valid shuffling does not exist, print the number $$$-1$$$ as the answer.If a valid shuffling exists, print $$$n$$$ space-separated integers — a permutation $$$p$$$ of $$$1,2,\ldots,n$$$ denoting a valid shuffling of shoes where the $$$i$$$-th student gets the shoes of the $$$p_i$$$-th student. If there are multiple answers, then print any of them.

Note: In the first test case, any permutation $$$p$$$ of $$$1,\ldots,n$$$ where $$$p_i\ne i$$$ would represent a valid shuffling since all students have equal shoe sizes, and thus anyone can wear anyone's shoes.In the second test case, it can be shown that no valid shuffling is possible.

Sample Inputs:
2
5
1 1 1 1 1
6
3 6 8 13 15 21

Sample Outputs:
5 1 2 3 4 
-1

Solution (Python):
t = int(input())
for i in range(t):
    n = int(input())
    s = input().split()
    if n == 1:
        print(-1)
        continue
    exists = True
    flag = 0
    prev = s[0]
    perms = []
    for j in range(1, n):
        if s[j] == prev:
            perms.append(str(j + 1))
            continue
        if flag == j - 1:
            exists = False
            break
        perms.append(str(flag + 1))
        prev = s[j]
        flag = j
    if flag == n - 1:
        exists = False
    perms.append(str(flag + 1))
    if not exists:
        print(-1)
    else:
        print(' '.join(perms))

Solution (Python):
def solve():
    if n == 1:
        return -1
    ans = []
    first = 0
    for i in range(1, n):
        if arr[i] == arr[first]:
            ans.append(i + 1)
        else:
            if i == first + 1:
                return -1
            ans.append(first + 1)
            first = i
    ans.append(first + 1)
    if first + 1 == n:
        return -1
    else:
        ans = ' '.join(map(str, ans))
        return ans
T = int(input())
for t in range(T):
    n = int(input())
    arr = list(map(int, input().split()))
    ans = solve()
    print(ans)

Solution (Python):
class Solution:

    def __init__(self):
        pass

    def solve(self):
        t = int(input())
        result = []
        for i in range(t):
            n = int(input())
            arr = list(map(int, input().split()))
            if n == 1:
                flag = False
            else:
                flag = True
            p = [-1]
            count = 1
            index = 1
            while index < n:
                if arr[index] == arr[index - 1]:
                    count += 1
                elif count == 1:
                    flag = False
                    break
                else:
                    count = 1
                    p.append(index - 1)
                index += 1
            shuffling = []
            if not flag or count == 1:
                shuffling = ['-1']
            else:
                p.append(index - 1)
                shuffling = ['0' for j in range(n)]
                for j in range(len(p) - 1):
                    diff = p[j + 1] - p[j]
                    func = lambda k, diff: (k + 2) % diff if k + 2 != diff else diff
                    for k in range(diff):
                        shuffling[p[j] + k + 1] = str(p[j] + func(k, diff) + 1)
            s = ' '.join(shuffling)
            result.append(s)
        for x in result:
            print(x)
if __name__ == '__main__':
    sol = Solution()
    sol.solve()