Chef Monocarp (Codeforces 1437C)
Tags: Greedy, graph matchings

Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?

Input: The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases.Then $$$q$$$ testcases follow.The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven.The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish.The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.

Output: Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.

Note: In the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.

Sample Inputs:
6
6
4 2 4 4 5 2
7
7 7 7 7 7 7 7
1
1
5
5 1 2 4 3
4
1 4 4 4
21
21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13

Sample Outputs:
4
12
0
0
2
21

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    n = int(input())
    T = list(map(int, input().split()))
    T.sort()
    dp = [float('inf')] * (n + 1)
    dp[n] = abs(T[n - 1] - 2 * n)
    for i in range(2 * n - 1, 0, -1):
        for j in range(max(i - n, 1), n):
            dp[j] = min(dp[j], dp[j + 1] + abs(i - T[j - 1]))
        dp[-1] = abs(T[n - 1] - i) if i >= n else float('inf')
    return dp[1]
for _ in range(int(input())):
    print(solve())

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    lst = list(map(int, input().strip().split()))
    lst.sort()
    dp = [float('inf')] * (n + 1)
    dp[0] = 0
    for i in range(1, 2 * n + 1):
        for j in range(n, 0, -1):
            dp[j] = min(dp[j], dp[j - 1] + abs(lst[j - 1] - i))
    print(dp[n])

Solution (Python):
MAXT = 400

def f(l):
    n = len(l)
    l.sort()
    dp = [0] * (MAXT + 1)
    for i in range(1, n + 1):
        t = l[i - 1]
        dpold = [i for i in dp]
        dp[i] = dpold[i - 1] + abs(i - t)
        for j in range(i + 1, MAXT + 1):
            dp[j] = min(dp[j - 1], dpold[j - 1] + abs(t - j))
    return min(dp[n:])
q = int(input())
for _ in range(q):
    n = int(input())
    l = list(map(int, input().split()))
    print(f(l))