Galaxytime limit:1000msmemory limit:262144kbthis problem'll be judged on HDU. Original id:5073
64-bit integer IO format: %i64d Java class name: Main Special JudgeGood 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 is tianming Yun and he gave it to Xin Cheng as a present.
To is fashionable, DRD also bought himself a galaxy. He named it Rho Galaxy. There is n stars in Rho Galaxy, and they has 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 had 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 the he can transport stars in a negligible time. After transportation, the N stars would also rotate around their new center of mass. Due to financial pressure, ATMs can only be transport at the most K stars. Since volumes of the stars are negligible, and both or more stars can is transported to the same position.
Now, your is supposed to calculate the minimum moment of inertia after transportation.InputThe first line contains an integer T (t≤10), denoting the number of the the test cases.
For each test case, the first line contains 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'll be is no more than 50000.OutputFor each test case, output one real number on one line representing the minimum moment of inertia. Your answer would be considered correct if and only if it 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
SourceAnshan Asia Regional Contest problem solving: Greedy mobile is better than not moving, K is always better than moving k-1.
1#include <bits/stdc++.h>2 using namespacestd;3 usingLL =Long Long;4 Const intMAXN =50010;5 LL S[MAXN],SS[MAXN],A[MAXN];6 intMain () {7 intkase,n,k;8scanf"%d",&Kase);9 while(kase--){Tenscanf"%d%d",&n,&k); One for(inti =1; I <= N; ++i) Ascanf"%i64d", A +i); - if(n = =k) { -Puts"0"); the Continue; - } -Sort (A +1, A + n +1); - for(inti =1; I <= N; ++i) { +S[i] = s[i-1] +A[i]; -Ss[i] = ss[i-1] + a[i]*A[i]; + } ALL ret =0x3f3f3f3f3f3f3f3f; at for(inti =0; I <= K; ++i) { -LL tmp = (ss[i + n-k]-ss[i]) * (nk); -ret = min (ret,tmp-(s[i + n-k]-s[i]) * (S[i + n-k]-s[i])); - } -printf"%.12f\n", ret*1.0/(nk)); - } in return 0; -}
View Code
HDU 5073 Galaxy