Title Link: HDU 5073 Galaxy
Surface:
GalaxyTime
limit:2000/1000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 2571 Accepted Submission (s): 642
Special Judge
Problem Descriptiongood 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
Source2014 Asia Anshan Regional Contest
Solving:
The position of the center of gravity is the average of all positions, and the moving points must eventually fall to the center of gravity. It is only necessary to find out which points are reserved, and the reserved points should be continuous, so as to ensure the minimum torque. Direct violence is n^2, and obviously it will time out. Because the requirements are, ∑ from 1 to N (d^2), and (Di-dave) ^2 can be split into di^2-2*di*dave+dave^2 can be pre-preprocessed di and di^2 accumulation and, when the final calculation of the direct fetch on the line. Because the initial value is too small, it leads to WA several times. It looks like it needs to be ordered.
Summarize:
1. Do not assume that the topics given are ordered.
2. For the maximum value, the initial value should be set to the first or last item.
Code:
#include <iostream> #include <cmath> #include <algorithm> #include <cstdio>using namespace std; int main () {int t,n,k; Long long int p[50000+10]; Long long int d[50000+10]; Long long int ds[50000+10]; Double Center; Double minn=0,temp; cin>>t; while (t--) {minn=9999999999999999; cin>>n>>k; for (int i=1;i<=n;i++) cin>>p[i]; Sort (p+1,p+n+1); if (n==k| | N-k==1) {printf ("0\n"); Continue } d[0]=0; ds[0]=0; for (int i=1;i<=n;i++) {d[i]=d[i-1]+p[i]; Ds[i]=ds[i-1]+p[i]*p[i]; } for (int i=0;i<=k;i++) {center= (D[i+n-k]-d[i]) *1.0/(n-k); Temp= (Ds[i+n-k]-ds[i]) -2*center* (D[i+n-k]-d[i]) + (n-k) *center*center; if (Temp<minn) minn=temp; } printf ("%.9f\n", Minn); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5073 Galaxy (greedy)