HDU 5073 Galaxy (greedy)

Source: Internet
Author: User

The main topic: give you n points, take away K, let you find out how to make the remaining inertia the smallest.

The idea of disintegration: greedy tactics. The remainder of the number is the smallest of the values, and the smallest inertia of n-k is calculated once and again, only when continuous.

PS: (x1-d) ^2+(x2-d) ^2+......+(xn-d) ^2 = SUM (x1^2+......+xn^2) +n*d*d-2*n (sum (X1+X2+......+XN)).

Galaxy Time limit:2000/1000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 2269 Accepted Submission (s): 548
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

#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MoD 1000000007using namespace std;const int maxn = 50110;double SUM1[MAXN], SUM2[MAXN];d ouble n    Um[maxn];int Main () {int T;    CIN >>T;        while (t--) {int n, K;        CIN >>n>>k;        for (int i = 1; I <= n; i++) scanf ("%lf", &num[i]);        Sort (num+1, num+1+n);        memset (sum1, 0, sizeof (SUM1));        memset (sum2, 0, sizeof (sum2));        int m = n-k;            if (!m) {printf ("0.0000000000\n");        Continue } Double Min = 1000000000000000000.0;            for (int i = 1; I <= n; i++) {sum1[i] = Sum1[i-1]+num[i];        Sum2[i] = Sum2[i-1]+num[i]*num[i];            } for (int i = 1; I <= n-m+1; i++) {int x = i;            int y = i+m-1;            Double d = (sum1[y]-sum1[x-1])/m*1.0;            Double TMP = (sum2[y]-sum2[x-1]) + m*1.0*d*d-2.0*d* (sum1[y]-sum1[x-1]);        min = min (min, tmp);    } printf ("%.10lf\n", Min); }}


HDU 5073 Galaxy (greedy)

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.