HDU 5073 Galaxy (数学)

Galaxy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4991    Accepted Submission(s): 1215
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.
 
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

 

题意:

  有n个点,可以删去k个点,使得公式 I 的值最小。

题解:

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long uLL;
15 #define ms(a, b) memset(a, b, sizeof(a))
16 #define pb push_back
17 #define mp make_pair
18 #define eps 0.0000000001
19 #define IOS ios::sync_with_stdio(0);cin.tie(0);
20 const LL INF = 0x3f3f3f3f3f3f3f3f;
21 const int inf = 0x3f3f3f3f;
22 const int mod = 1e9+7;
23 const int maxn = 50000+10;
24 double a[maxn];
25 double sum[maxn];
26 double sum_pow[maxn];
27 void solve()
28 {
29     ms(sum, 0);ms(sum_pow, 0);
30     int n, k;scanf("%d%d", &n, &k);
31     for(int i = 1;i<=n;i++)  scanf("%lf", &a[i]);
32     sort(a+1, a+1+n);
33     for(int i = 1;i<=n;i++){
34         sum[i] = sum[i-1] + a[i];
35         sum_pow[i] = sum_pow[i-1] + a[i]*a[i];
36     }
37     if(n-k<=1){
38         printf("0.0000000000
");return;
39     }
40     double ans = (double)INF;
41     for(int i = n-k;i<=n;i++){
42         double A = (double)(n-k);
43         double B = -2.0*(sum[i]-sum[i-(n-k)]);
44         double C = sum_pow[i] - sum_pow[i-(n-k)];
45         double x = -B/(2.0*A);
46 //        printf("%f %f %f %f
", A, B, C, x);
47 //        printf("%f
", A*x*x + B*x + C);
48 
49         ans = min(ans, A*x*x + B*x + C);
50     }
51     printf("%.10f
", ans);
52     return;
53 }
54 int main() {
55 #ifdef LOCAL
56     freopen("input.txt", "r", stdin);
57 //        freopen("output.txt", "w", stdout);
58 #endif
59 //    IOS
60     int t;scanf("%d", &t);
61     while(t--)
62         solve();
63     return 0;
64 }
View Code
原文地址:https://www.cnblogs.com/denghaiquan/p/7257775.html