Numbers Exchange (Codeforces 746E)
Tags: Implement, greedy

Eugeny has n cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct. Nikolay has m cards, distinct numbers from 1 to m are written on them, one per card. It means that Nikolay has exactly one card with number 1, exactly one card with number 2 and so on. A single exchange is a process in which Eugeny gives one card to Nikolay and takes another one from those Nikolay has. Your task is to find the minimum number of card exchanges and determine which cards Eugeny should exchange.

Input: The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 109) — the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that n is even.The second line contains a sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the numbers on Eugeny's cards.

Output: If there is no answer, print -1.Otherwise, in the first line print the minimum number of exchanges. In the second line print n integers — Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the i-th card wasn't exchanged then the i-th number should coincide with the number from the input data. Otherwise, it is considered that this card was exchanged, and the i-th number should be equal to the number on the card it was exchanged to.If there are multiple answers, it is allowed to print any of them.


Sample Inputs:
6 2
5 6 7 9 4 5

8 6
7 7 7 7 8 8 8 8

4 1
4 2 1 10

Sample Outputs:
1
5 6 7 9 4 2

6
7 2 4 6 8 1 3 5

-1

Solution (Python):
import sys

def read_input():
    f = sys.stdin
    (n, m) = map(int, f.readline().split())
    a = list(map(int, f.readline().split()))
    return (n, m, a)

def solve(n, m, a):
    v = []
    visited = set()
    counter = {0: 0, 1: 0}
    for i in range(1, n + 1):
        a_i = a[i - 1]
        if a_i in visited or counter[a_i % 2] == n // 2:
            v.append(i - 1)
        else:
            visited.add(a_i)
            counter[a_i % 2] += 1
    ans = 0
    for i in range(1, m + 1):
        if ans == len(v):
            break
        if i in visited:
            continue
        if counter[i % 2] < n // 2:
            counter[i % 2] += 1
            a[v[ans]] = i
            ans += 1
    if ans < len(v):
        print(-1)
    else:
        print(ans)
        print(' '.join(map(str, a)))

def main():
    solve(*read_input())
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math

def read_input(input_path=None):
    if input_path is None:
        f = sys.stdin
    else:
        f = open(input_path, 'r')
    (n, m) = map(int, f.readline().split())
    a = list(map(int, f.readline().split()))
    return (n, m, a)

def sol(n, m, a):
    c = []
    nc = []
    d = {}
    for i in range(n):
        xi = a[i]
        if xi in d:
            continue
        d[xi] = True
        if xi % 2 == 0:
            c.append((xi, i))
        else:
            nc.append((xi, i))
    (a, b) = (-1, 0)
    if len(nc) > len(c):
        (c, nc) = (nc, c)
        (a, b) = (b, a)
    c = sorted(c, key=lambda x: -x[0])
    d = {}
    r = [0] * n
    for i in range(len(nc)):
        (xi, j) = nc[i]
        r[j] = xi
        d[xi] = True
    for i in range(min(n // 2, len(c))):
        (xi, j) = c[i]
        r[j] = xi
        d[xi] = True
    j = 0
    ans = 0
    for i in range(len(c), n // 2):
        b += 2
        while b in d:
            b += 2
        while r[j] != 0:
            j += 1
        r[j] = b
        ans += 1
    for i in range(len(nc), n // 2):
        a += 2
        while a in d:
            a += 2
        while r[j] != 0:
            j += 1
        r[j] = a
        ans += 1
    if a > m or b > m:
        return ['-1']
    return [f'{ans}', ' '.join(map(str, r))]

def solve(input_path=None):
    return sol(*read_input(input_path))

def main():
    for line in sol(*read_input()):
        print(f'{line}')
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math

def main():
    (n, m) = map(int, sys.stdin.readline().split())
    x = list(map(int, sys.stdin.readline().split()))
    c = []
    nc = []
    d = {}
    for i in range(n):
        xi = x[i]
        if xi in d:
            continue
        d[xi] = True
        if xi % 2 == 0:
            c.append((xi, i))
        else:
            nc.append((xi, i))
    (a, b) = (-1, 0)
    if len(nc) > len(c):
        (c, nc) = (nc, c)
        (a, b) = (b, a)
    c = sorted(c, key=lambda x: -x[0])
    d = {}
    r = [0] * n
    for i in range(len(nc)):
        (xi, j) = nc[i]
        r[j] = xi
        d[xi] = True
    for i in range(min(n // 2, len(c))):
        (xi, j) = c[i]
        r[j] = xi
        d[xi] = True
    j = 0
    ans = 0
    for i in range(len(c), n // 2):
        b += 2
        while b in d:
            b += 2
        while r[j] != 0:
            j += 1
        r[j] = b
        ans += 1
    for i in range(len(nc), n // 2):
        a += 2
        while a in d:
            a += 2
        while r[j] != 0:
            j += 1
        r[j] = a
        ans += 1
    if a > m or b > m:
        print(-1)
        return
    print(ans)
    print(' '.join(map(str, r)))
main()