首页 > 代码库 > HDU 5073 Galaxy(居然是暴力)
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 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.
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.
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
2014 Asia AnShan Regional Contest
题意:
给你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(居然是暴力)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。