hdu 5073 Galaxy(2014acm鞍山亚洲分部 C)
主题链接: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

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.
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.
2
3 2
-1 0 1
4 2
-2 -1 1 2
0
0.5
pid=5080" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5080
pid=5079" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5079
5077 5076pid=5073" style="color:rgb(26,92,200); text-decoration:none">Statistic
#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)的更多相关文章
- hdu 5073 Galaxy(2014acm鞍山亚洲分部 D)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5073 Galaxy(2014鞍山赛区现场赛D题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...
- HDU 5073 Galaxy (2014 Anshan D简单数学)
HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...
- hdu 5073 Galaxy(2014 鞍山现场赛)
Galaxy Time Limit: 2000/1000 MS (J ...
- 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy
Galaxy Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...
- ACM学习历程—HDU 5073 Galaxy(数学)
Description Good news for us: to release the financial pressure, the government started selling gala ...
- HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total S ...
- hdu 5073 Galaxy 数学 铜牌题
0.5 题意:有n(n<=5e4)个质点位于一维直线上,现在你可以任意移动其中k个质点,且移动到任意位置,设移动后的中心为e,求最小的I=(x[1]-e)^2+(x[2]-e)^2+(x[3]- ...
- HDU 5073 Galaxy (数学)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
随机推荐
- Swift - 文本标签(UILabel)的用法
1,标签的创建 1 2 3 4 5 6 7 8 9 10 import UIKit class ViewController: UIViewController { override func ...
- 带着项目学PHP第九讲 - 如何给ecshop的wap版本首页和商品页添加商品图片
ecshop的wap版本自身不带图片, 所以看起来光秃秃的,非常不讨人喜欢, 网络上关于wap的模板就不像pc版那么多,容易找到, 而且能找到的都是要花钱买的, 虽然这个小小的改动不能替代找个合适的模 ...
- html中加入超链接方式的汇总
在CSS样式中,对超链接的样式有以下几种定义(1)设置链接未被访问时的样式,具体写法如下:a:link{font-size:10px;... }(2)设置链接在鼠标经过时的样式,具体写法如下:a:ho ...
- 改变Edit的光标(使用CreateCaret,ShowCaret和LoadBitmap三个API函数)
看着Edit的光标,是不是觉得了无生趣,想不想换个形状来玩玩,其实很简单,且听我道来. Edit是Windows的标准控件,它是一个系统范围窗口类,所以任何应用程序都能创建它.其实Edit本质上也是一 ...
- 浅析Delphi Container库(有开源的DCLX)
与Java和C++相比,Delphi对容器的支持实在少得可怜.Java有强大的集合框架,C++更有STL,Delphi有什么呢,不就是TList几个小巧的列表类,而TCollection系列的类更多只 ...
- HDU 3473 Minimum Sum (划分树)
题意:给定一个数组,有Q次的询问,每次询问的格式为(l,r),表示求区间中一个数x,使得sum = sigma|x - xi|最小(i在[l,r]之间),输出最小的sum. 思路:本题一定是要O(nl ...
- jQuery EasyUI API 中文文档 - 菜单按钮(menubutton)
<html> <head> <script src="jquery-easyui/jquery.min.js"></script> ...
- VC6迁移到VS2008几个问题——良好的代码,从我做起,从现在开始。
最近.有一个项目开发,需要使用一次项目的代码.只有当项目VC6下编译通过的,在VS2008下不一定编译通过,能编译通过也不一定能链接成功.以下总结一下我在一个VC6项目移植到VS2008中遇到的一些问 ...
- BUG系列:转让startActivityForResult()&onActivityResult()没有反应
前天遇到了一个麻烦,还真是麻烦啊. 我使用startActivityForResult()&onActivityResult().由Activity-A 跳转到Activity-B 页面,然后 ...
- Android源代码分析-资源载入机制
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/23387079 (来自singwhatiwanna的csdn博客) 前言 我们 ...