hdoj5073Galaxy【方差】
来源:互联网 发布:大学公选课网络课 编辑:程序博客网 时间:2024/04/30 02:12
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.
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.
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.
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
23 2-1 0 14 2-2 -1 1 2
Sample Output
00.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 0x3f3f3f3fusing 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;}
0 0
- hdoj5073Galaxy【方差】
- 方差
- 方差
- 方差
- 方差
- 方差
- 方差
- 方差
- 方差
- 方差,標准差,協方差
- 方差、标准差和协方差
- 方差、标准差和协方差
- 求方差
- allan方差
- 方差公式
- luoguP1471 方差
- 方差、协方差和协方差矩阵
- 样本方差与总体方差
- Mac下Jenkins安装(一)
- Best Time to Buy and Sell Stock with Cooldown
- Maven项目显示红叹号
- 流体布局CSS3中 calc()的使用 顺带box-sizing
- LintCode-简单-字符串查找
- hdoj5073Galaxy【方差】
- UICollectionView: 1>UICollectionViewFlowLayout流式布局
- *LeetCode 29. Divide Two Integers
- iOS 图像开发之Quartz 2D
- 这一段时间的事
- Java Swing小结
- Introduction to the SQL Server Analysis Services Logistic Regression Data Mining Algorithm
- 详解CSS position属性
- 设计模式系列12---遍历统一的迭代模式