ACM学习历程—HDU 5073 Galaxy(数学)

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 Rho Galaxy.
There are n stars in Rho Galaxy, 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 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 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 w i is the weight of star i, d i 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

2

3 2

-1 0 1

4 2

-2 -1 1 2

Sample Output

0

0.5

题目大意就是在n个数里面找n-k个数,然后让他们的方差*(n-k)最小。

首先D(x)
= E(x^2) – E(x)^2

但是方差还有个定义:

由这个式子可以发现是一个关于an的二次函数,当前n-1个点的方差知道时,第n个点加入时,当第n个点越远离前n-1个点的重心,整体的方差越大。

于是对所有点排序,每次都连续取n-k个点,取里面最小的。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long

using namespace std;

const int maxN = 50005;
int n, k, a[maxN], d[maxN<<1], top;

void quickSort()
{
    int len = 0;
    for (int i = 0; i <= top; ++i)
    {
        while (d[i])
        {
            a[len++] = i-maxN;
            d[i]--;
        }
    }
}

void input()
{
    memset(d, 0, sizeof(d));
    scanf("%d%d", &n, &k);
    int tmp;
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &tmp);
        tmp += maxN;
        d[tmp]++;
        if (i == 0 || top < tmp)
            top = tmp;
    }
    k = n-k;
}

void work()
{
    double ans;
    if (k == 0)
        ans = 0;
    else
    {
        quickSort();
        double e2 = 0, e = 0;
        for (int i = 0; i < k; ++i)
        {
            e2 += (LL)a[i]*a[i];
            e += a[i];
        }
        ans = e2/k-e/k*e/k;
        for (int i = k; i < n; ++i)
        {
            e2 += (LL)a[i]*a[i]-(LL)a[i-k]*a[i-k];
            e += a[i]-a[i-k];
            ans = min(ans, e2/k-e/k*e/k);
        }
    }
    printf("%.10lf\n", ans*k);
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 0; times < T; ++times)
    {
        input();
        work();
    }
    return 0;
}
时间: 2024-10-06 07:28:08

ACM学习历程—HDU 5073 Galaxy(数学)的相关文章

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

hdu 5073 Galaxy(数学)

题目链接:hdu 5073 Galaxy 题目大意:给定N个点,可以移动其中的K的点,问说最后I的最小值可以是多少. 解题思路:因为质量都为1嘛,所以就是求方差,可以移动K个,所以即选连续的n=N-K个使得方差最小.注意N=K的情 况. S表示n个数的和,T表示n个数平方的和,那么这n个数的方差即为T - S * S / n,然后扫描一遍数组维护S,T,并且计算 方差的最小值. #include <cstdio> #include <cstring> #include <al

ACM学习历程—HDU5587 Array(数学 &amp;&amp; 二分 &amp;&amp; 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后从0到末尾,每一个都加上1. 例如:a0, a1, a2 => a0, a1, a2, 1, a0+1, a1+1, a2+1 题解中是这么说的:“ 其实Ai为i二进制中1的个数.每次变化A{k+2^i}=A{k}+1,(k<2^?i??)不产生进位,二进制1的个数加1.然后数位dp统计前m个数二

ACM学习历程—HDU 3915 Game(Nim博弈 &amp;&amp; xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

ACM学习历程—HDU 5534 Partial Tree(动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x1)+f(x2)+...+f(xn)的最大值. 首先由于是树,所以有n-1条边,然后每条边连接两个节点,所以总的度数应该为2(n-1). 此外每个结点至少应该有一个度. 所以r1+r2+...rn = 2n-2.ri >= 1; 首先想到让ri >= 1这个条件消失: 令xi = ri,则x1+x

ACM学习历程—HDU 5536 Chip Factory(xor &amp;&amp; 字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. 此外题目中说大数据只有10组,小数据最多n只有100.(那么c*n^2的复杂度应该差不多) 于是可以考虑枚举i和j,然后匹配k. 于是可以先把所有s[k]全部存进一个字典树, 然后枚举s[i]和s[j],由于i.j.k互不相等,于是先从字典树里面删掉s[i]和s[j],然后对s[i]+s[j]这个

ACM学习历程—HDU 3949 XOR(xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的值都求出来,对于这个规模的n是不可行的. 然后之前有过类似的题,求最大的,有一种方法用到了线性基. 那么线性基能不能表示第k大的呢? 显然,因为线性基可以不重复的表示所有结果.它和原数组是等价的. 对于一个满秩矩阵 100000 010000 001000 000100 000010 000001

ACM学习历程—HDU1030 Delta-wave(数学)

Description A triangle field is numbered with successive integers in the way shown on the picture below. The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only,