贪心:

保存连续的n-k个数,求最小的一段方差。。。

。预处理O1算期望。

。。

Galaxy

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

Total Submission(s): 752    Accepted Submission(s): 176

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
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=50050; double sum[maxn],sum2[maxn];
int a[maxn];
int n,k; double FC(int from,int to)
{
double n=to-from+1.;
double V=(sum[to]-sum[from-1])/n;
double A=sum2[to]-sum2[from-1];
double B=sum[to]-sum[from-1];
return A-2*V*B+n*V*V;
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
}
if(k==n||k==n-1)
{
puts("0.0000000000");
continue;
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+1.*a[i];
sum2[i]=sum2[i-1]+1.*a[i]*a[i];
}
int m=n-k;
double ans=1e30;
for(int i=1;i+m-1<=n;i++)
{
int j=i+m-1;
ans=min(ans,FC(i,j));
}
printf("%.10lf\n",ans);
}
return 0;
}

HDOJ 5073 Galaxy 数学 贪心的更多相关文章

  1. hdu 5073 Galaxy 数学 铜牌题

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

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

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

  3. hdu 5073 Galaxy(2014acm鞍山亚洲分部 C)

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

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

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

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

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

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

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

  7. HDU 5073 Galaxy (数学)

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

  8. HDU - 5073 Galaxy(数学)

    题目 题意:n个点,运行移动k个点到任何位置,允许多个点在同一位置上.求移动k个点后,所有点到整体中心的距离的平方和最小. 分析:这题题目真的有点迷...一开始看不懂.得知最后是选取一个中心,于是看出 ...

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

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

随机推荐

  1. IO流基础

    IO流,也称为数据流,用于处理设备之间的数据传输. JAVA对数据的操作就是通过流的方式,而流分为两种:字符流,字节流 字符流: 可以内部制定码表,处理文字很方便,字符流里的基类是Reader,Wri ...

  2. Xcode 使用自定义字体

    添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...

  3. 反射给对象赋值遇到的问题——类型转换[转http://blog.csdn.net/xiaohan2826/article/details/8536074]

    发布时间:2012-10-25 10:49浏览次数:225 给一个对象属性赋值可以通过PropertyInfo.SetValue()方式进行赋值,但要注意值的类型要与属性保持一致.   创建对象实例的 ...

  4. 手机端QQ客服直接跳转到QQ

    企业QQ呼出QQ对话框方法 1.手机端链接是这样的:mqqwpa://im/chat?chat_type=wpa&uin=386807630&version=1&src_typ ...

  5. 滚动条响应鼠标滑轮事件实现上下滚动的js代码

    <script type="text/javascript"> var scrollFunc=function(e){ e=e || window.event; if( ...

  6. angular post发送请求和GET发送请求,服务器端接收不到信息的问题

    参数可能因为编码原因,服务器端无法接收到传递的值, 这时需要用到补丁来解决这个问题 1,下载一个http.patch.js文件,放入YII框架中的js/ng文件架内 2angularjs 创建模型部分 ...

  7. EasyUI篇のDataGrid

    HTML: <table id="dg"></table> 或者 <div id="dg"></div> JS: ...

  8. CodeFirst中DB保存时报错:对一个或多个实体的验证失败。

    错误提示如下: 开始以为有字段可能没有添加数据,可是检查了很久,仍然没有任何头绪. 后使用DbEntityValidationException进行调试,问题才得以解决

  9. div+css知识点

    前端书写规范: 1.所有书写均在英文半角状态下的小写: 2.id,class必须以字母开头: 3.所有标签必须闭合: 4.html标签用tab键缩进: 5.属性值必须带引号: 6.<!-- ht ...

  10. study note -(some good sentence)

    There are, however, some disadvantages.However, there may be better ways of tacking this problem. Ho ...