WOJ1023 - Division

Given a set S with n numbers , we wish to divide these numbers into ?clusters? so that:
(1) Each cluster contains at least p numbers; 
(2) Each cluster contains consecutive number from list ;
(3) The sum of the squared deviation of the numbers from their cluster mean is as small as possible. Let(|S| is
the size of the set)denotes the mean of a set of numbers forming a cluster. For a number in the cluster, its squared deviation from the
cluster mean is .

Given n and p described as above, please write a program to calculate the minimum sum of the squared deviation of the numbers from
their cluster mean. 

输入格式

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number

of test cases.
Each test case begins with a line containing two integers n (1 <= n <= 100) and p (1 <= p <= n), where n represents the size of the set and p represents the least number of elements each cluster must contain. The second line contains positive integers (you are ensured
that the value of each integer is less than ), which are separated by a white space denoting the elements of the set.

输出格式

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting

from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
For each test case, you should output one line containing a floating number, which is the minimum sum of the squared deviation of the numbers from their cluster mean. The answer should be accurate to two fractional digits.

样例输入

2
2 1
1 2
2 2
1 2

样例输出

Case 1:
0.00

Case 2:
0.50

一开始的时候,本人题目理解错了

这个division是要把set全部元素划分为满足题述的三个条件的clusters

每个cluster的元素个数可能大于p

让所有clusters的sum of the squared deviation最小

#include 
#include 
#include 
#include 
using namespace std;
int array[110];
double sigma[110][110], f[110], sign[110][110];
int main()
{
    int T, Ti;
    cin >> T;
    for(Ti=1;Ti<=T;Ti++)
    {
        int n, p,i,j,k;
        memset(sign, 0, sizeof sign);
        scanf("%d %d", &n, &p);
        for (i = 1; i <= n; ++i)
            scanf("%d", &array[i]);
        for (i = 1; i <= n; ++i)//用sigma矩阵的上半边存储i~j的累和 
            for (j = i; j <= n; ++j)
            {
                if (j == i)
                    sigma[i][j] = array[j];
                else sigma [i][j] = sigma[i][j - 1] + array[j];
            }
        for (i = 1; i <= n; ++i)//用sign矩阵的上半边存储i~j的sum of the squared deviation 
            for (j = i; j <= n; ++j)
            {
                double mid = sigma[i][j] / (j - i + 1);
                for (k = i; k <= j; ++k)
                    sign[i][j] += pow(array[k] - mid,2);
            }
        for (i = 1; i <= n; ++i)//设定一个极大的数 
            f[i] = 1e35;
        f[0] = 0;
        for (i = 1; i <= n; ++i)
            for (j = 0; j < i; ++j)
            {
                if (i - j < p)
                    continue;
                f[i] = min(f[i], f[j] + sign[j + 1][i]);//用动态规划求解最小的squared deviation 
            }
        if (Ti!=1)
            puts("");
        printf("Case %d:\n",Ti);
        printf("%.2lf\n", f[n]);//f[n]里就是最终答案 
    }
    return 0;
}


你可能感兴趣的:(WOJ)