HDOJ 5073 Galaxy 数学 贪心
贪心:
保存连续的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
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
#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 数学 贪心的更多相关文章
- hdu 5073 Galaxy 数学 铜牌题
0.5 题意:有n(n<=5e4)个质点位于一维直线上,现在你可以任意移动其中k个质点,且移动到任意位置,设移动后的中心为e,求最小的I=(x[1]-e)^2+(x[2]-e)^2+(x[3]- ...
- 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(2014acm鞍山亚洲分部 C)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others) ...
- 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(Anshan 2014)(数学推导,贪婪)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total S ...
- ACM学习历程—HDU 5073 Galaxy(数学)
Description Good news for us: to release the financial pressure, the government started selling gala ...
- HDU 5073 Galaxy (数学)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
- HDU - 5073 Galaxy(数学)
题目 题意:n个点,运行移动k个点到任何位置,允许多个点在同一位置上.求移动k个点后,所有点到整体中心的距离的平方和最小. 分析:这题题目真的有点迷...一开始看不懂.得知最后是选取一个中心,于是看出 ...
- 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy
Galaxy Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...
随机推荐
- ie11下,插入框架Html
if(navigator.userAgent.indexOf("MSIE")>0 || (navigator.userAgent.indexOf("Trident& ...
- JQuery zoom插件学习
jquery zoom是一款图片放大插件,经常用在商城商品页面里. 使用JQuery zoom插件,除了需要引入JQuery.js外,还要引入JQuery.zoom.js文件及jqzoom.css文件 ...
- 【Linux】vi编辑器命令
1)进入vi的命令 vi filename : 打开或新建文件,并将光标置于第一行首 vi +n filename : 打开文件,并将光标置于第n行首 ...
- PHP面向对象(OOP):__call()处理调用错误
在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...
- 运行在TQ2440开发板上以及X86平台上的linux内核编译
一.运行在TQ2440开发板上的linux内核编译 1.获取源码并解压 直接使用天嵌移植好的“linux-2.6.30.4_20100531.tar.bz2”源码包. 解压(天嵌默认解压到/opt/E ...
- iOS: 获取文件路径
iOS: 获取文件路径 // 例如 - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectories ...
- [BZOJ 1874] [BeiJing2009 WinterCamp] 取石子游戏 【博弈论 | SG函数】
题目链接:BZOJ - 1874 题目分析 这个是一种组合游戏,是许多单个SG游戏的和. 就是指,总的游戏由许多单个SG游戏组合而成,每个SG游戏(也就是每一堆石子)之间互不干扰,每次从所有的单个游戏 ...
- 基于LeanCloud云引擎的Web全栈方案
LeanEngine-Full-Stack The FULL STACK DEVELOPER 复杂的项目, 协作分工, 自动化流程,代码组织结构,框架选择,国际化方案等 Generator 或者See ...
- 24篇Delphi文件操作文章
http://www.cnblogs.com/keyvip/category/268043.html
- 1.Repeater控件
在用到数据库数据并且要逐条显示时,就需要用到repeater\listview等这样的数据库控件进行动态的显示数据. Repeater相当于foreach的功能,用于对绑定数据源中的数据进行遍历显示, ...