您的位置:首页 > 其它

2014ACM/ICPC亚洲区鞍山赛区现场赛D Galaxy(hdu 5073)

2014-10-23 09:10 489 查看


Galaxy

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 645 Accepted Submission(s): 146

Special Judge


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

2
3 2
-1 0 1
4 2
-2 -1 1 2


Sample Output

0
0.5


题意:本来有n个星球,让你移去k个星球,然后使剩下的(n-k)个球到中心点的平方和最小。

做法:sum=(pos[i]-center)^2 展开就是pos[i]^2-2*pos[i]*center+center^2。所以(n-k)加起来就是Σ(x→n-k+x)pos[i]^2-2*pos[i]*center+center^2,然后x的范围是[0,k],所以最后能转化成sum和sum2和center的多项式,然后进行k次比较就ok了,具体看代码。

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL  long long
#define inf 0x0f0f0f0f
#define maxn 4
using namespace std;
struct number
{
int t;
__int64 x;
__int64 y;
}num[1005];
bool cmp(struct number a,struct number b)
{
return a.t<b.t;
}
int main()
{
int t;
int n;
double ans;
double tt;
int i;
scanf("%d",&t);
while(t--)
{
ans=-10000;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%I64d%I64d",&num[i].t,&num[i].x,&num[i].y);
sort(num,num+n,cmp);
for(i=1;i<n;i++)
{
tt=(num[i].x-num[i-1].x)*(num[i].x-num[i-1].x);
tt+=(num[i].y-num[i-1].y)*(num[i].y-num[i-1].y);
tt=sqrt(tt);
tt=tt/(num[i].t-num[i-1].t);
if(tt>ans)
ans=tt;
}
printf("%.10f\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: