D - Galaxy

题目描述

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.
T:测试组数
n:星球个数,k:可以移走的星球个数
n个整数:每个星球的位置(在一条直线上)
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

题意:
有n颗行星,我们可以移走k颗,
让 公式中的 I 最小,也就是要求di^2求和最小(wi是重量,题目中说了各星球质量一致)
di的均值用e表示(e就是旋转轴在的位置)
di^2求和 实质就是 输入的n个数的方差
让方差尽可能小,也就是让数据尽可能聚拢在一块,毫无疑问,能移走k块我们不可能移走k+1块!
然后剩下的就是m=n-k块了。

未避免文字不清楚,附一张公式图
在这里插入图片描述
用前缀和计算某一区间段的和

前缀和
S[i] = a[1] + a[2] + … a[i]
a[l] + … + a[r] = S[r] - S[l - 1]

代码:

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

using namespace std;
const int N=5*1e4+10;
double eps=1e-9;
long long loc[N];
long long s1[N],s2[N];//前缀和 

void solve()
{
	int n,k;
	scanf("%d%d",&n,&k);
	int m=n-k;
	
	for(int i=1;i<=n;i++)
		scanf("%lld",&loc[i]);
	//输入的位置未必有序,先排序 
	sort(loc+1,loc+n+1);
	//预处理前缀和 
	for(int i=1;i<=n;i++){
		s1[i]=s1[i-1]+loc[i];
		s2[i]=s2[i-1]+loc[i]*loc[i];
	}
	
	double ans=1e18;
	double sum=0;
	if(m==0){cout<<0<<"\n";return;}
	for(int i=m;i<=n;i++){
		//i-m+1 到 i共m个 
		double sum1=s1[i]-s1[i-m];
		double e=sum1*1.0/m;//e均值 
		sum=s2[i]-s2[i-m]-2*e*sum1+m*e*e;
		ans=min(ans,sum);
	}
	//保证精度! 
	printf("%.12lf\n",ans);
	return;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--){
		memset(loc,0,sizeof loc);
		memset(s1,0,sizeof s1);
		memset(s2,0,sizeof s2);
		solve();
	}
	return 0;
}

最后,题目有精度要求!直接用cout输出到小数点后6位WA了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值