hdoj5073Galaxy【方差】
2016-01-03 21:26
253 查看
Galaxy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3747 Accepted Submission(s): 896
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.
![](http://acm.hdu.edu.cn/data/images/C549-1004-2.jpg)
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
![](http://acm.hdu.edu.cn/data/images/C549-1004-1.jpg)
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
题意:计算方差;利用方差的简化计算公式暴力
我刚开始利用方差的意义反应样本波动的大小做的但是一直wa不知道为什么
利用方差的简化计算公式
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn=50010; double num[maxn]; int main() { int t,i,j,k,n; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&k); for(i=0;i<n;++i){ scanf("%lf",&num[i]); }k=n-k; if(k==0){ printf("0\n"); continue; } double sum=0,avg=0; sort(num,num+n); for(i=0;i<k;++i){ avg+=num[i]; sum+=(num[i]*num[i]); } double d=sum-avg*avg/k; double temp,stemp=sum,savg=avg; for(i=k;i<n;++i){ stemp=stemp+num[i]*num[i]-num[i-k]*num[i-k]; savg=savg+num[i]-num[i-k]; temp=stemp-savg*savg/k; if(temp<d)d=temp; } printf("%.10lf\n",d); } return 0; }
我的利用方差的性质做的一直wa
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #define inf 0x3f3f3f3f using namespace std; const int maxn=50010; double num[maxn]; bool cmp(double a,double b){ return a<b; } int main() { int t,i,j,n,k; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&k); for(i=0;i<n;++i){ scanf("%lf",&num[i]); } sort(num,num+n,cmp); k=n-k; if(k==0){ printf("0\n"); continue; } int l=0,r=k-1; double sum=0; for(i=1;i<k;++i){ sum+=fabs(num[i]-num[i-1]); } double temp=sum; for(i=k;i<n;++i){ temp=temp+fabs(num[i]-num[i-1])-fabs(num[i-k+1]-num[i-k]); if(temp<sum){ r=i;l=i-k+1; sum=temp; } } double avg=0; for(i=l;i<=r;++i){ avg+=num[i]; } double ans=0; avg=avg/k; for(i=l;i<=r;++i){ ans=ans+(num[i]-avg)*(num[i]-avg); } printf("%.10lf\n",ans); } return 0; }
相关文章推荐
- 设计模式系列12---遍历统一的迭代模式
- Linux入门教程:CDN加速
- iOS 图像开发之Quartz 2D
- 169,使用VVDocumenter-Xcode插件实现注释文档格式
- 这一段时间的事
- XML序列化与反序列化
- 详解CSS position属性
- 【机房合作】甘特图
- Java Swing小结
- 英语中逗号的使用
- html头部中的meta里到底有些什么?
- linux网络管理初探
- leetcode - Binary Tree Postorder Traversal
- 图片的拉伸问题,修改frame的方式,
- OK6410用DNW下载led和ds18b20的裸机程序
- 深入浅出Java类和对象的初始化
- UICollectionView: 1>UICollectionViewFlowLayout流式布局
- Linux命令之修改主机名
- 流体布局CSS3中 calc()的使用 顺带box-sizing
- Maven项目显示红叹号