HDU 5073 Galaxy (Greedy)
Question:
Galaxy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission (s): 2571 Accepted Submission (s): 642
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 rock Galaxy. there are n stars in neogalaxy, 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 enough t 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
23 2-1 0 14 2-2 -1 1 2
Sample Output
00.5
Source 2014 Asia AnShan Regional Contest
Solution:
The mean of all positions when the center of gravity is located, and the points that can be moved are definitely on the center of gravity. Therefore, you only need to find out which are the reserved points, and the reserved points should be continuous, so as to ensure the minimum sum of torque. Direct brute force is n ^ 2, which obviously times out. Because the requirement is that Σ ranges from 1 to n (d ^ 2), while (di-dave) ^ 2 can be split into di ^ 2-2 * di * dave + dave ^ 2. You can pre-process the accumulation and sum of di and di ^ 2, and take the result directly. Because the initial values are too small, wa is performed multiple times. It seems to be required, in the descending order.
Summary:
1. Do not think that the question is given in an orderly manner.
2. Calculate the maximum value. The initial value should be the first or last value.
Code:
#include
#include
#include #include
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); 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