Chef Monocarp
链接 : http://codeforces.com/problemset/problem/1437/C
思路 :
dp方程 : dp[i][k] = min(dp[i][k], dp[i - 1][j] + abs(k - a[i])).
dp[i][j] 表示对于第i道菜, 在j时刻取出.
若第i - 1道菜在j时刻取出, 第i道取出的时间应该从j + 1开始枚举.
代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi firsqt
#define se second
#define inf 0x3f3f3f3f
const int N = 210;
int f[2 * N][2 * N];
int a[N];
int main() {
IO;
int _;
cin >> _;
while (_--) {
memset(f, 0x3f, sizeof f);
for (int i = 0; i < 2 * N; ++i) f[0][i] = 0;
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 2 * N; ++j)
for (int k = j + 1; k < 2 * N; ++k)
f[i][k] = min(f[i][k], f[i - 1][j] + abs(k - a[i]));
int ans = inf;
for (int i = 1; i < 2 * N; ++i) ans = min(f[n][i], ans);
cout << ans << endl;
}
return 0;
}