HDU 2014 Anshan division 5073 galaxy

Source: Internet
Author: User

Meaning: give you n numbers, representing the positions of N planets. Each planet has a weight of 1!
At the beginning, every planet rotates around the center of gravity. The center of gravity is the sum of the positions of all planets/number of planets.
Now let you move K planets to any location (multiple planets can be in the same location and all planets are in the same straight line)
After moving, the locations of their centers may change, and the I = sum (di ^ 2) di (representing the distance from the first planet to the center of the center) is the smallest!

If D is the center of n-k planets, if the I value is the smallest, then the moving K planets must be placed in the hearts of the other n-k planets,
And the n-k planets must be continuous! The more intensive, the smaller the variance .....
X1, x2, X3, X4,... x (n-k) indicates the position of the remaining n-k planets
Thought: I = sum (di ^ 2) = (x1-d) ^ 2 + (x2-d) ^ 2 + (x3-d) ^ 2 ....
= Sum (Xi ^ 2) + (n-k) * D-2 * D * sum (XI );

1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 # include <algorithm> 5 # define n 50050 6 using namespace STD; 7 double num [N]; 8 double S1 [N], S2 [N]; 9 int main () {10 int N, T, K; 11 scanf ("% d", & T); 12 while (t --) {13 scanf ("% d", & N, & K ); 14 For (INT I = 1; I <= N; ++ I) 15 scanf ("% lf", & num [I]); 16 sort (Num + 1, num + n + 1); // rank 17 for the position of the planet (INT I = 1; I <= N; ++ I) // calculate the sum of the prefixes num [I] and the sum of num [I] ^ 2 and 18 S1 [I] = S1 [I-1] + num [I], s2 [I] = S2 [I-1] + num [I] * num [I]; 19 int M = n-k; 20 double ans = 1000000000000000000.0; // ans should be large enough .... it is better not to use long, which may exceed .... 21 For (INT I = 1; M & I + M-1 <= N; ++ I) {22 Int J = I + s-1; 23 double D = (S1 [J]-S1 [I-1])/m; 24 Double TMP = S2 [J]-S2 [I-1]-2 * D * (S1 [J]-S1 [I-1]) + M * D; 25 if (ANS> TMP) ans = TMP; 26} 27 if (n = k) ans = 0.0; 28 printf ("%. 9lf \ n ", ANS); 29} 30 return 0; 31}
View code

 

HDU 2014 Anshan division 5073 galaxy

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.