hdu 5073 Galaxy

来源:互联网 发布:情义知多少 编辑:程序博客网 时间:2024/04/23 23:05

Problem Description
Good news for us: to release the financial pressure, the government started selling galaxies and we can buy them from now on! The first one who bought a galaxy was Tianming Yun and he gave it to Xin Cheng as a present.


To be fashionable, DRD also bought himself a galaxy. He named it Rho Galaxy. There are n stars in Rho Galaxy, and they have the same weight, namely one unit weight, and a negligible volume. They initially lie in a line rotating around their center of mass.

Everything runs well except one thing. DRD thinks that the galaxy rotates too slow. As we know, to increase the angular speed with the same angular momentum, we have to decrease the moment of inertia.

The moment of inertia I of a set of n stars can be calculated with the formula


where wi is the weight of star i, di is the distance form star i to the mass of center.

As DRD’s friend, ATM, who bought M78 Galaxy, wants to help him. ATM creates some black holes and white holes so that he can transport stars in a negligible time. After transportation, the n stars will also rotate around their new center of mass. Due to financial pressure, ATM can only transport at most k stars. Since volumes of the stars are negligible, two or more stars can be transported to the same position.

Now, you are supposed to calculate the minimum moment of inertia after transportation.
 

Input
The first line contains an integer T (T ≤ 10), denoting the number of the test cases.

For each test case, the first line contains two integers, n(1 ≤ n ≤ 50000) and k(0 ≤ k ≤ n), as mentioned above. The next line contains n integers representing the positions of the stars. The absolute values of positions will be no more than 50000.
 

Output
For each test case, output one real number in one line representing the minimum moment of inertia. Your answer will be considered correct if and only if its absolute or relative error is less than 1e-9.
 

Sample Input
23 2-1 0 14 2-2 -1 1 2
 

Sample Output
00.5

在一个数轴上有n个点,给出坐标,你最多可以移动k个点到任意位置,使得移动后所有点到这n个点的重心的平方和最小。

分析:重心即为这n个点的算术平均值,到重心的距离的平方和。。。先看看方差公式

方差:QQ图片20141022214508

,其中为方差,EX为算术平均值,为平方后的平均值

我们要求的就是的最小值

还有一点,总是最先移动的是离重心最远的点,可能是左边,可能是右边,还可能部分左边,部分右边,反正就是两边,并且剩余的点肯定是连续的,这样可以对数据进行排序,在枚举。


#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;int main(){    int t,N,k;    scanf("%d",&t);    double num[50005];    while (t--)    {        scanf("%d%d",&N,&k);        for (int i=0; i<N; i++)        {            scanf("%lf",&num[i]);        }        if(N==k)        {            printf("%.10lf\n",0.0);            continue;        }        sort(num, num+N);        double ans=0;        double t=0,s=0;        int n=N-k;        for (int i=0; i<n; i++)  //先去掉最右边的k个点        {            t+=num[i]*num[i];            s+=num[i];        }        ans=t-s*s/n*1.0;        for (int i=n; i<N; i++)   //枚举各种端点情况        {            t+=num[i]*num[i]-num[i-n]*num[i-n];            s+=num[i]-num[i-n];            ans=min(ans,t-s*s*1.0/n);        }        printf("%.10lf\n",ans);    }    return 0;}




0 0