码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5073 Galaxy(居然是暴力)

时间:2014-10-23 22:51:27      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:数学

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.

bubuko.com,布布扣

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

bubuko.com,布布扣

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
2 3 2 -1 0 1 4 2 -2 -1 1 2
 

Sample Output
0 0.5
 

Source
 

题意:

   给你n个点,排序后可以删除m个点,然后找一个点(可以不是这n个点),求所有点到这个点的距离的平方和


思路:暴力枚举左边删除几个点,右边删除几个点,然后求出所求取优

    但是怎么求所求呢?假设那个点为t,那么就是(x1-t)^2+(x2-t)^2+.....(xn-t)^2  

我这里取n为3时方便  就是x1*x1 +x2*x2 +x3*x3 -2(x1+x2+x3)*t+3*t*t,此时最优的答案 t  是(x1+x2+x3)/3    (注意题目是n个点删除m个,所以程序中应把3换成 n-m )

然后把t带入就可以了


详情看代码,不懂就看上面的式子


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define INF 0x3f3f3f3f
#define N  50005

double a[N],sum[N],temp[N];
int n,m;

double fdd(int s,int e) //s是左边删除的点个数,e是右边删除的点个数
{
    int i,j;
	s=1+s;   //算出左起点
	e=n-e;   //算出右终点
	double t=(temp[e]-temp[s-1])/(n-m);
	return sum[e]-sum[s-1]-2*(temp[e]-temp[s-1])*t+(n-m)*t*t; //不懂看上面的式子
}

int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		sum[0]=temp[0]=0;

		for(i=1;i<=n;i++)
		{
			scanf("%lf",&a[i]);
		}

       sort(a+1,a+n+1);
       for(i=1;i<=n;i++)
	   {
	   	  sum[i]=sum[i-1]+a[i]*a[i];
		  temp[i]=temp[i-1]+a[i];
	   }

		double ans=fdd(0,m);

		if(n-m<=1)
		{
			printf("0.0000000000\n");
			continue;
		}

		for(i=0;i<=m;i++)
			ans=min(ans,fdd(i,m-i));

		printf("%.10f\n",ans);
	}
    return 0;
}







HDU 5073 Galaxy(居然是暴力)

标签:数学

原文地址:http://blog.csdn.net/u014737310/article/details/40402577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!