-
Notifications
You must be signed in to change notification settings - Fork 185
/
moogle.cc
48 lines (41 loc) · 1.03 KB
/
moogle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// https://open.kattis.com/problems/moogle
#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
typedef vector<int> vi;
double MAX = 210000000;
double cache[201][201][201];
double error(const vi &a, int i, int j) {
double r = 0;
for (int k = i + 1; k < j; k++) {
double x = a[i] + (a[j] - a[i]) * double(k - i) / double (j - i);
r += fabs(x - a[k]);
}
return r;
}
double s(const vi &a, int c, int i, int j, int k) {
if (j == a.size() - 1) return error(a, i, j);
if (cache[i][j][k] >= 0) return cache[i][j][k];
double e = MAX;
if (k < c - 1) e = min(e, error(a, i, j) + s(a, c, j, j + 1, k + 1));
if (a.size() - j > c - k) e = min(e, s(a, c, i, j + 1, k));
cache[i][j][k] = e;
return e;
}
int main() {
int t;
cin >> t;
while (t--) {
for (int i = 0; i < 201; i++)
for (int j = 0; j < 201; j++)
for (int k = 0; k < 201; k++)
cache[i][j][k] = -1;
int h, c;
cin >> h >> c;
vi a(h);
for (int i = 0; i < h; i++) cin >> a[i];
cout << s(a, c, 0, 1, 1) / h << endl;
}
}