hdu Galaxy(数学公式)
来源:互联网 发布:阿里云服务器访问外网 编辑:程序博客网 时间:2024/06/05 04:21
Galaxy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 775 Accepted Submission(s): 180
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
2 3 2 -1 0 1 4 2 -2 -1 1 2
Sample Output
0 0.5
·此题中间运算好像会有溢出(50000*50000) ,所以这里的输入需要使用double 或者 I64
· 注意运算式子的化简,否则是无法使用前缀和的。
Code:
1 //============================================================================ 2 // Name : 1.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream>10 #include <algorithm>11 #include <stdio.h>12 #include <string.h>13 #include <math.h>14 #include <stdlib.h>15 #include <queue>16 using namespace std;17 18 double f[50010];19 20 int t,n,k;21 22 int main() {23 //freopen("in.txt","r",stdin);24 25 scanf("%d",&t);26 while(t--){27 scanf("%d %d",&n,&k);28 for(int i=0;i<n;i++) scanf("%lf",&f[i]);29 sort(f,f+n);30 double tot=0,num=0,mid;31 for(int i=0;i<n-k;i++) tot+=f[i];32 if(n!=k) mid = tot/(n-k);33 else{34 printf("0\n");continue;35 }36 for(int i=0;i<n-k;i++) num += (f[i]-mid)*(f[i]-mid);37 //printf("%f %f \n",num,mid);38 double maxc = num;39 for(int i=0;i<k;i++){40 num -= f[i]*f[i];41 num += 2*tot*mid;42 num -= mid*mid*(n-k);43 44 tot -= f[i]; tot += f[i+n-k];45 mid = tot/(n-k);46 47 num += f[i+n-k]*f[i+n-k];48 num -= 2*tot*mid;49 num += mid*mid*(n-k);50 51 if(num < maxc) maxc = num;52 //printf("%f %f \n",num,mid);53 }54 printf("%.10f\n",maxc);55 }56 57 return 0;58 }
0 0
- hdu Galaxy(数学公式)
- HDU 5073 Galaxy(数学公式)
- HDU - 5073 Galaxy
- hdu 5073 Galaxy
- HDU 5073 Galaxy
- Galaxy (hdu 5073 数学)
- hdu 5073 Galaxy(数学)
- HDU 5073Galaxy
- hdu 5073 Galaxy
- HDU 5073 Galaxy
- HDU 5073 Galaxy(贪心)
- -----hdu 5073-Galaxy
- hdu 1046 数学公式推导
- hdu 1049 数学公式推导
- hdu 1018 数学公式
- hdu 2114 数学公式!!!
- hdu 3299 Distant Galaxy 策略
- hdu 5073——Galaxy
- 最短路(转)模板
- vs2012 error c4996: 'fopen': This function or variable may be unsafe
- 高斯消元法---模板(转自kuangbin)
- 单源最短路模板(转自 海子)
- hdu Hatsune Miku(DP)
- hdu Galaxy(数学公式)
- Java IO流学习
- 81Widget小控件
- POJ题目推荐(转载)
- C++ 优先队列(转)
- POJ 1011 递归 & 回溯
- 2014-11-20动态规划:顺推法与逆推法中递推公式的不同!
- POJ:1050(枚举 + DP)
- 2014-11-24-就业事务中心-C语言考试培训