HDU 5073 Galaxy(居然是暴力)

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 w i is the weight of star i, d i 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;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值