HDU 5073 Galaxy (Issue D of the 2014 Anshan Field Competition)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.