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 = ;
int n, k, a[maxN], d[maxN<<], top; void quickSort()
{
int len = ;
for (int i = ; i <= top; ++i)
{
while (d[i])
{
a[len++] = i-maxN;
d[i]--;
}
}
} void input()
{
memset(d, , sizeof(d));
scanf("%d%d", &n, &k);
int tmp;
for (int i = ; i < n; ++i)
{
scanf("%d", &tmp);
tmp += maxN;
d[tmp]++;
if (i == || top < tmp)
top = tmp;
}
k = n-k;
} void work()
{
double ans;
if (k == )
ans = ;
else
{
quickSort();
double e2 = , e = ;
for (int i = ; 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 = ; times < T; ++times)
{
input();
work();
}
return ;
}

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

  1. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

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

    Description A triangle field is numbered with successive integers in the way shown on the picture be ...

  8. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

  9. ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

随机推荐

  1. Android 与H5之间的js交互

    之前项目做过一些Android和Html5之间js交互方面的东西,今天有时间就总结一下: 一.为什么要进行js交互: 为了方便原生开发和Html之间数据传递,在静态页面的情况下可以改变原生开发的页面: ...

  2. vue实现简易留言板

    首先引入vue.js <script src="vue.js"></script> 布局 <div id="div"> &l ...

  3. output value . Sigmoid neurons are similar to perceptrons, but modified so that small changes in their weights and bias cause only a small change in their output.

    http://neuralnetworksanddeeplearning.com/chap1.html . Sigmoid neurons are similar to perceptrons, bu ...

  4. PHP开发环境搭建(转载)

    转载自:http://blog.csdn.net/rosetta/article/details/53967215 前言   最近学了n种语言,学每种语言的套路无非就是先搭建一个开发环境,再找本书或者 ...

  5. python cookbook第三版学习笔记十五:property和描述

    8.5 私有属性: 在python中,如果想将私有数据封装到类的实例上,有两种方法:1 单下划线.2 双下划线 1 单下划线一般认为是内部实现,但是如果想从外部访问的话也是可以的 2 双下划线是则无法 ...

  6. 一对多 添加表单 cocoon

    gem 'cocoon' - javascript "cocoon.js" https://note.youdao.com/web/#/file/XCiivnE/note/WEB4 ...

  7. c的详细学习(10)结构体与共用体的学习(二)

    在c语言中,结构体数据类型与共用体数据类型都属于构造类型.共用体与结构体数据类型在定义上十分相似,但它们在存储空间的占用分配上有本质的区别.结构体变量是各种类型数据的集合,各成员占据不同的存储空间,而 ...

  8. 深入理解ES6之迭代器与生成器

    迭代器 迭代器 iterator,在 Javascript 中,迭代器是一个对象(也可称作为迭代器对象),它提供了一个 next() 方法,用来返回迭代序列中的下一项. next 方法的定义,next ...

  9. 斐波那契 (Fibonacci)数列

    尾递归会将本次方法的结果计算出来,直接传递给下个方法.效率很快. 一般的递归,在本次方法结果还没出来的时候,就调用了下次的递归, 而程序就要将部分的结果保存在内存中,直到后面的方法结束,再返回来计算. ...

  10. ML一(概念学习和一般到特殊序)

    概念学习和一般到特殊序 Concept Learning and the General-To-Specific Ordering 1 简介 1.1 定义 概念学习(Concept Learning) ...