Galaxy

Galaxy

Given in the one-dimensional coordinate axis n points, the i-th coordinate point \ (x_i \) , you can now move any k points, minimizing their variances, \ (n \ Leq 50000 \) .

solution

Set \ (\ bar {X} = \ FRAC {\ sum_. 1 = {I}} ^ {n-nx_i} \) , readily known variance

\[\sum_{i=1}^n(x_i-\bar{x})^2=\sum_{i=1}^nx_i^2-n\bar{x}^2\]

For moving only one view point coordinates x, as will be apparent to a quadratic function, i.e.,

\(f(x)=\sum_{i=1}^n{x_i}^2-n\bar{x}^2\)

Since simplification is too complex, it is evaluated guide, so independent of x items can be lost

\[f(x)'=(\sum_{i=1}^n{x_i}^2-n\bar{x}^2)'=2x-n(\bar{x}^2)'=2x-n2\bar{x}(\bar{x})'=\]
\[2x-2\bar{x}(\sum_{i=1}^nx_i)'=2x-2\bar{x}\]

Easy to know with increasing x, the guide function is a monotonically increasing, it exists a minimum value, at the stagnation point is clearly to take, so it \ (F (x) '= 0 \) , there

\(x=\bar{x}\Rightarrow x=\frac{\sum_{i=1}^nx_i-x}{n-1}\)

So for just a move in terms of coordinates x, x to get the average point other than the point where x is the answer, we can get a conclusion to the set number added to its average, does not change the new collection average.

But we need to discuss the case k points, so for each point \ (x_i \) we have (Let's assume that k points to \ (x_1, x_2, .., x_k \) ), respectively, then

\[x_1=\frac{\sum_{i=1}^nx_i-x_1}{n-1},x_2=\frac{\sum_{i=1}^nx_i-x_2}{n-1}...\]
\[x_k=\frac{\sum_{i=1}^nx_i-x_k}{n-1}\]

There add up

\[(n-1)\sum_{i=1}^kx_i=k\sum_{i=1}^nx_i-\sum_{i=1}^kx_i\]

That is

\[n\sum_{i=1}^kx_i=k\sum_{i=1}^nx_i\]

That is

\[\frac{\sum_{i=1}^kx_i}{k}=\frac{\sum_{i=1}^nx_i}{n}\]

So we get the average number of k to be movable exactly the average of the number of n, continues to get

\[n\sum_{i=1}^kx_i=k\sum_{i=1}^nx_i\Rightarrow n\sum_{i=1}^kx_i=k\sum_{i=1}^kx_i+k\sum_{i=k+1}^nx_i\]

\[(n-k)\sum_{i=1}^kx_i=k\sum_{i=k+1}^nx_i\]

That is

\[\frac{\sum_{i=1}^kx_i}{k}=\frac{\sum_{i=k+1}^nx_i}{n-k}\]

There are also finishing up

\[\frac{\sum_{i=1}^kx_i}{k}=\frac{\sum_{i=k+1}^nx_i}{n-k}=\frac{\sum_{i=1}^nx_i}{n}\]

So a great conclusion can be drawn, adding a number to the average of a set for the change of the average of the set, the same average, equal to the average of the original collection added to the average number equal to the average of the new collection and meet the two are equal, it can launch three equal.

Now consider the number k of variance, variance apparently converted to better study the following formula

\[\sum_{i=1}^nx_i^2-n\bar{x}^2\]

Nk the remaining number is constant (including averages), have open

\[\sum_{i=1}^kx_i^2+\sum_{i=k+1}^{n}x_i^2-n\bar{x}^2\]

So let the whole equation is minimized, which is minimized

\[\sum_{i=1}^nx_i^2=x_1^2+x_2^2+...+x_k^2\]

According mean inequality, easy to understand minimize this formula, to meet the \ (x_1 = x_2 = ... = x_k \) .

And we have \ (\ {FRAC x_1 + x_2 + ... + K x_k}} = {\ FRAC kx_1} {} {K = x_1 = \ bar {X} \) , so there

\[x_1=x_2=...=x_k=\bar{x}=\frac{\sum_{i=k+1}^{n-k}x_i}{n-k}\]

K is for this number, we just put all the rest of the digital location to the average, according to the definition of variance style.

\[\sum_{i=1}^n(x_i-\bar{x})^2\]

Easy to know, this time where the k numbers generated is zero, because \ (x_i = \ bar {x } (i = 1,2, ..., k) \)

So long as we consider the rest nk-digit variance can be, and according to the degree of dispersion of the actual meaning of the variance, digital, so the remaining nk digits to concentrate as much as possible, so we only need to coordinate all sort of take continuous length as \ (nk \) range, the variance of them can take the max, in order to quickly find the variance, according to our promotion formula

\[\sum_{i=1}^nx_i^2-n\bar{x}^2\]

And \ (\ bar {X} = \ FRAC {\ sum_. 1 = {I}} ^ {n-nx_i} \) , so long as we maintain common prefix and prefixes and square and, into this equation calculation, it can do \ (O (1) \) query interval variance, the whole time complexity is only \ (O (the n-) \) .

Reference Code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define il inline
#define ri register
#define lb double
#define Size 50500
using namespace std;
lb a[Size],sum[Size],sum2[Size];
int main(){
    int lsy,n,k;scanf("%d",&lsy);
    while(lsy--){
        scanf("%d%d",&n,&k);
        for(int i(1);i<=n;++i)
            scanf("%lf",&a[i]);
        if(n==k){printf("%.11lf\n",(lb)0);continue;}
        sort(a+1,a+n+1);lb ans(1e100);k=n-k;
        for(int i(1);i<=n;++i)
            sum[i]=sum[i-1]+a[i],
                sum2[i]=sum2[i-1]+a[i]*a[i];
        for(int i(k);i<=n;++i)
            ans=min(ans,sum2[i]-sum2[i-k]-k*pow((sum[i]-sum[i-k])/k,2));
        printf("%.11lf\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11408652.html