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颗 ...
随机推荐
- 利用CMake自己创建OpenCV静态链接库
1.准备工作: 1)完成Visual Studio2012安装: 2)下载并解压CMake3.5.0: 3)下载并解压OpenCV2.4.12: 4)下载并解压TBB44_20160128oss. 2 ...
- c++文件读写相关
在看C++编程思想中,每个练习基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含义,在看了几位大牛的博文后,进行整理和总结: 这里主要是讨论fstream的内容: ...
- SGU 124.Broken line
时间限制:0.25s 空间限制:4M 题意: 给出n条线段和一个点,保证所有线段平行X轴或Y,并且闭合成一个多边形.判断这个点的位置是在多边形上,还是多边形内,还是多边形外. solution: 由于 ...
- SGU 175.Encoding
Solution: 简单题. 答案初始化为1. 从给定的n,q往下推出新的n和q,如果q是在右半边,答案加上 n-n/2. 一直到推到n==1. code: #include <iostream ...
- java浮点数剖析
定点数表达法的缺点在于其形式过于僵硬,固定的小数点位置决定了固定位数的整数部分和小数部分,不利于同时表达特别大的数或者特别小的数.计算机系统采纳了所谓的浮点数表达方式.这种表达方式利用科学计数法来表达 ...
- windows7 64 位 mysql 5.6.12 安装
1.修改 my-default.ini 改名为 my.ini 内容修改为 [mysqld] loose-default-character-set = utf8 basedir = D:/mys ...
- css中文字体乱码解决方案
css中文字体乱码解决方案:把css编码和html页面编码统一起来.如果html页面是utf-8.css.js也统一成utf-8编码.还有一个避免中文乱码的办法就是把中文字体写成英文来表示 css中文 ...
- Nginx fastcgi_param解释
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...
- BZOJ 1031 字符加密
Description 喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作 ...
- SSH:Connection closed by foreign host
以为和防火墙,性能,HOSTS.DENY,端口之类的有关,后来查了下,啥都没有关系. 就是同一台机器NAT之后,被另一台抢了先机. http://blog.sina.com.cn/s/blog_6d0 ...