2014-03-03 08:08:21Morris

[UVA][DP] 11370 - Moogle



  I: Moogle 

You got the original idea of making map software, called Moogle Maps, for the new cool Maple mPhone. It will even be capable of indicating the location of a house address like ``Main Street 13". However, since the mPhone has limited storage capacity, you need to reduce the data amount. You don't want to store the exact location of every single house number. Instead only a subset of the house numbers will be stored exactly, and the others will be linearly interpolated. So you want to select house numbers that will minimise the average interpolation error, given how many house locations you have capacity to store. We view the street as a straight line, and you will always store the first and the last house location.

epsfbox{p11370.eps}

Given that you've stored the locations xi and xj for the houses with numbers i and j respectively, but no other house in between, the interpolated value for a house with number k with i < k < j is xi + (xj - xi) . $ {frac{{k-i}}{{j-i}}}$

Input 

The first line of input gives a single integer, 1$ le$t$ le$50 , the number of test cases.

For each test case, there are two lines. The first contains 2$ le$h$ le$200 and 2$ le$c$ le$h , where h is the number of houses in the street and c is the number of house locations that can be stored. The second contains h integers in increasing order giving the location of the h houses. Each location is in the interval [0, 1000000].

Output 

For each test case, output the average interpolation error over all the h houses for the optimal selection of c house locations to store. The output should be given with four decimal places, but we will accept inaccuracies of up to ± 0.001.

Sample Input 

2 
4 3 
0 9 20 40 
10 4 
0 10 19 30 40 90 140 190 202 210

Sample Output 

0.2500
0.3000



題目描述:

數線上 N 個點,取用 M 個點,求與內插法誤差平均最小的值為何 ?

題目解法:


dp[i][j] 表示前 i 個數字選用 j 個點的最小誤差總和。
// 最難理解的是內插法給的公式,一開始看不是很懂,原來是要對每一個實際值去扣掉得到誤差。

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

double dp[205][205], w[205][205], x[205];
int main() {
    int testcase;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        int n, m;
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; i++)
            scanf("%lf", &x[i]);
        for(i = 0; i < n; i++) {
            for(j = i+1; j < n; j++) {
                double c = 0;
                for(k = i+1; k < j; k++)
                    c += fabs(x[i] + (x[j] - x[i])*(k - i)/(double)(j - i) - x[k]);
                w[i][j] = c;
            }
        }
        for(i = 0; i <= n; i++)
            for(j = 0; j <= m; j++)
                dp[i][j] = 1e+30;
        dp[0][1] = 0;
        for(i = 0; i < n; i++) {
            for(j = 1; j <= m; j++) {
                for(k = i+1; k <= n; k++) {
                    dp[k][j+1] = min(dp[k][j+1], dp[i][j] + w[i][k]);
                }
            }
        }
        printf("%.4lf\n", dp[n-1][m]/n);
    }
    return 0;
}