主题链接:http://acm.hdu.edu.cn/showproblem.php?

pid=5073

Galaxy

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

Total Submission(s): 768    Accepted Submission(s): 179

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
 
Source
 
Recommend
 

pid=5073" style="color:rgb(26,92,200); text-decoration:none">Statistic | Submit | Discuss | Note

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 50100
using namespace std;
int T,N,K;
int pos[MAXN],sum[MAXN],x[MAXN],First,Last;
double DL,DR,mass;
int PL,PR,need;
double ans,NowI;
double GetNext(){
double ret=NowI;
double pmass=mass;
mass=(sum[Last+1]-sum[First]+0.0)/need;
NowI+=-(pmass-pos[First])*(pmass-pos[First])+(mass-pos[Last+1])*(mass-pos[Last+1]);
NowI+=(mass-pmass)*(mass-pmass)*(need-1);
DL-=(pmass-pos[First]);
Last++;First++;
int t=PL-First+1;
NowI+=2*DL*(mass-pmass);
DL+=(mass-pmass)*t;
PL++;
while(PL<=N && pos[PL]<=mass){
DL+=fabs(mass-pos[PL]);
DR-=fabs(pmass-pos[PL]);
NowI+=(mass-pos[PL])*(mass-pos[PL]);
NowI-=(pmass-pos[PL])*(pmass-pos[PL]);
NowI-=(mass-pmass)*(mass-pmass);
PL++;
}
NowI+=2*(pmass-mass)*DR;
DR+=(pos[Last]-mass);
PL--;
DR-=(mass-pmass)*(Last-(PL+1));
return NowI;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&K);
for(int i=0;i<N;i++)
scanf("%d",&pos[i]);
sort(pos,pos+N);
sum[0]=pos[0];
for(int i=1;i<N;i++)
sum[i]=sum[i-1]+pos[i];
need=N-K;ans=0;PL=0;
if(N==1 || need <2 ){
puts("0.000000000000");
continue;
}
double kkk=sum[need-1]*1.0/need;First=0;Last=need-1;
DL=0;PL=0;DR=0;NowI=0;
for(int i=0;i<need;i++){
NowI+=(kkk-pos[i])*(kkk-pos[i]);
if(pos[i]<=kkk){
PL=i;
DL+=(kkk-pos[i]);
}
else
DR+=(pos[i]-kkk);
}
mass=kkk;
ans=NowI;
while(Last<N-1){
GetNext();
ans=min(ans,NowI);
}
printf("%.12lf\n",ans);
}
}

hdu 5073 Galaxy(2014acm鞍山亚洲分部 C)的更多相关文章

  1. hdu 5073 Galaxy(2014acm鞍山亚洲分部 D)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)   ...

  2. HDU 5073 Galaxy(2014鞍山赛区现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...

  3. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  4. hdu 5073 Galaxy(2014 鞍山现场赛)

    Galaxy                                                                   Time Limit: 2000/1000 MS (J ...

  5. 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy

    Galaxy Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...

  6. ACM学习历程—HDU 5073 Galaxy(数学)

    Description Good news for us: to release the financial pressure, the government started selling gala ...

  7. HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total S ...

  8. hdu 5073 Galaxy 数学 铜牌题

    0.5 题意:有n(n<=5e4)个质点位于一维直线上,现在你可以任意移动其中k个质点,且移动到任意位置,设移动后的中心为e,求最小的I=(x[1]-e)^2+(x[2]-e)^2+(x[3]- ...

  9. HDU 5073 Galaxy (数学)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

随机推荐

  1. ocx控件避免弹出警告的类--2

    本文与 OCX控件避免弹出安全警告的类 http://www.cnblogs.com/lidabo/archive/2013/03/26/2981852.html 有些类似,只不过增加了几行代码(红色 ...

  2. 低头看—SQL视频

    迷迷糊糊半个月过去了,耿大姐的数据库视频也在一知半解中看完.虽然耿大妈讲的很详细,很细心,但是我还是时不时的犯困(还不如看儿童英语动画片有精神呢)视频看是看完了,但东西不是自己的.这个时候就需要“颗粒 ...

  3. ibatis新手入门

    ibatis 是什么 iBATIS是以SQL为中心的持久化层框架. 能支持懒载入.关联查询.继承等特性. iBATIS不同于一般的OR映射框架. OR映射框架,将数据库表.字段等映射到类.属性,那是一 ...

  4. Swift 可展开可收缩的表视图

    主要学习与运行效果 在本节的内容中,我们将通过一个具体实例的实现过程,详细讲解在表视图当中,如何创建一个可展开可收缩的表视图.为了让读者有着更为直观的印象,我们将通过模仿QQ好友列表来实现这个效果. ...

  5. google 搜索url详解

    www.google.com [http://www.google.cn/search?q=112&hl=zh-CN&client=aff- 360daohang&hs=yhE ...

  6. 忽然想到:把Mu的源代码一网打尽

    那么那些流媒体开发的公司,就不会拒绝我了,真是一举两得.

  7. php获取server端mac和clientmac的地址

    获取servermac <?php /** 获取网卡的MAC地址原码:眼下支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetmacAddr{ var $re ...

  8. Eclipse插件引入jar包的方法

    搞了两天,终于找到解决办法了.原来  Eclipse 插件项目引入外面的jar包不能用   build path---->add external jars的方法. 先说明两个概念:类加载器,O ...

  9. hdu1116--解题报告--初步了解欧拉回路

    由题目意思..我们只要把n个字符串的首尾字母看作是点,这个字符串看着边来处理就可以啦...将题目的案例图形化如下: 那么接着就是欧拉路径和欧拉回路判断,我们这里用并査集来判断图是不是连通的,然后根据有 ...

  10. LIS(最长的序列)和LCS(最长公共子)总结

    LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...