Chef Monocarp has just put nn dishes into an oven. He knows that the ii-th dish has its optimal cooking time equal to titi 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−ti| — the absolute difference between TT and titi. 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 qq (1≤q≤2001≤q≤200) — the number of testcases.
Then qq testcases follow.
The first line of the testcase contains a single integer nn (1≤n≤2001≤n≤200) — the number of dishes in the oven.
The second line of the testcase contains nn integers t1,t2,…,tnt1,t2,…,tn (1≤ti≤n1≤ti≤n) — the optimal cooking time for each dish.
The sum of nn over all qq testcases doesn't exceed 200200.
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.
Example
input
Copy
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
output
Copy
4 12 0 0 2 21
Note
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.
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
const int mod=1e9+7;
ll t,n,l,r,q,p;
ll cnt;
ll a[maxn];
ll dp[maxn];
ll poww(ll a,ll b){
ll ans=1,base=a;
while(b!=0){
if(b&1!=0)
ans*=base;
base*=base;
b>>=1;
}
return ans;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
dp[i]=mod;
}
sort(a+1,a+1+n);
for(int i=1;i<=2*n;i++)
{
for(int j=n;j>=1;j--)
{
dp[j]=min(dp[j],dp[j-1]+abs(a[j]-i));
}
}
cout<<dp[n]<<endl;
}
return 0;
}

此问题涉及安排烹饪时间以最小化Chef Monocarp的菜肴不满意度。每个菜肴都有一个最佳烹饪时间,需要在特定时刻取出以使不满意度最小。输出是在所有测试用例中,当所有菜肴按最佳时间取出时的最小总不满意度。
255

被折叠的 条评论
为什么被折叠?



