Link: HDU 5073 galaxy
In a one-dimensional coordinate system, give the coordinates of N points and rotate K points, minimize the inertia of the galaxy after rotation (the smallest inertia is the minimum distance from all the stars to the center of the galaxy, which can be understood as the least square difference ). Minimum inertia.
Ideas:
First sort the sequence, and then find the recurrence formula for calculating the inertia of N-K points.
Take three examples:
Preprocessing is the sum of squares of the mean and items,
Note: N = K
AC code:
# Include <stdio. h ># include <algorithm> using namespace STD; double pf [50010], sum [50010]; Double A [50010]; int main () {int t; int N, k, I; double Ave, minans, tempfc; scanf ("% d", & T); While (t --) {memset (PF, 0, sizeof PF ); memset (sum, 0, sizeof sum); scanf ("% d", & N, & K); for (I = 0; I <n; I ++) scanf ("% lf", & A [I]); If (n = k) {printf ("% lf \ n", 0); continue ;} sort (A, A + n); pf [0] = A [0] * A [0], sum [0] = A [0]; for (I = 1; I <n; I ++) {pf [I] = pf [I-1] + A [I] * A [I]; sum [I] = + sum [I-1] + A [I];} // preprocessing Ave = sum [n-k-1] * 1.0/(n-k ); minans = pf [n-k-1]-2 * Ave * sum [n-k-1] + (n-k) * Ave; for (I = n-k; I <N; I ++) {Ave = (sum [I]-sum [I-(n-k)])/(n-k ); tempfc = (PF [I]-PF [I-(n-k)]) -2 * Ave * (sum [I]-sum [I-(n-k)]) + (n-k) * Ave; minans = min (minans, tempfc);} printf ("% lf \ n", minans);} return 0 ;} /* 103 2-1 0 14 2-2-1 1 23 3-1 0 1 */
HDU 5073 Galaxy (Issue D of the 2014 Anshan Field Competition)