CF1437C.Chef Monocarp

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Chef Monocarp has just put nn dishes into an oven. He knows that the ii-th dish has its optimal cooking time equal to tit_i minutes.

At any positive integer minute TT Monocarp can put no more than one dish out of the oven. If the ii-th dish is put out at some minute TT, then its unpleasant value is ∣T−ti∣|T - t_i| — the absolute difference between TT and tit_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?

厨师 Monocarp 刚刚将 nn 道菜放入烤箱中。他已知第 ii 道菜的最佳烹饪时间为 tit_i 分钟。

在任意正整数分钟 TT,Monocarp 最多只能从烤箱中取出一道菜。若第 ii 道菜在某一分钟 TT 被取出,则其不适值(unpleasant value)为 ∣T−ti∣|T - t_i| —— 即 TT 与 tit_i 的绝对差值。一旦某道菜被取出烤箱,便不可再放回。

Monocarp 必须将所有菜肴都从烤箱中取出。Monocarp 能获得的最小总不适值是多少?

输入格式

The first line contains a single integer qq (1≤q≤2001 \le q \le 200) — the number of testcases.

Then qq testcases follow.

The first line of the testcase contains a single integer nn (1≤n≤2001 \le n \le 200) — the number of dishes in the oven.

The second line of the testcase contains nn integers t1,t2,…,tnt_1, t_2, \dots, t_n (1≤ti≤n1 \le t_i \le n) — the optimal cooking time for each dish.

The sum of nn over all qq testcases doesn't exceed 200200.

第一行包含一个整数 qq(1≤q≤2001 \le q \le 200)——测试用例的数量。

接下来是 qq 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤2001 \le n \le 200)——烤箱中菜肴的数量。

每个测试用例的第二行包含 nn 个整数 t1,t2,…,tnt_1, t_2, \dots, t_n(1≤ti≤n1 \le t_i \le n)——每道菜肴的最佳烹饪时间。

所有 qq 个测试用例的 nn 值之和不超过 200200。

输出格式

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.

对每个测试用例,输出一个整数——Monocarp 将所有菜肴从烤箱中取出时所能得到的最小总不愉快值。注意:Monocarp 只能在正整数分钟时刻取出菜肴,且任意一分钟最多只能取出一道菜。

输入输出样例

  • 输入#1

    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

    输出#1

    4
    12
    0
    0
    2
    21

说明/提示

In the first example Monocarp can put out the dishes at minutes 3,1,5,4,6,23, 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|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,104, 5, 6, 7, 8, 9, 10.

In the third example Monocarp can put out the dish at minute 11.

In the fourth example Monocarp can put out the dishes at minutes 5,1,2,4,35, 1, 2, 4, 3.

In the fifth example Monocarp can put out the dishes at minutes 1,3,4,51, 3, 4, 5.

在第一个例子中,Monocarp 可以在第 3,1,5,4,6,23, 1, 5, 4, 6, 2 分钟端出菜肴。此时总不适值为 ∣4−3∣+∣2−1∣+∣4−5∣+∣4−4∣+∣6−5∣+∣2−2∣=4|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4。

在第二个例子中,Monocarp 可以在第 4,5,6,7,8,9,104, 5, 6, 7, 8, 9, 10 分钟端出菜肴。

在第三个例子中,Monocarp 可以在第 11 分钟端出菜肴。

在第四个例子中,Monocarp 可以在第 5,1,2,4,35, 1, 2, 4, 3 分钟端出菜肴。

在第五个例子中,Monocarp 可以在第 1,3,4,51, 3, 4, 5 分钟端出菜肴。

输入解题思路,AI测评打分。不知道怎么写?

首页