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. lua注册函数

    #include <stdio.h> #include <math.h> #define MAX_COLOR 255 extern "C" { #inclu ...

  2. 【python】-- 初识python

    Python 安装 windows: 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机 ...

  3. 我的Android进阶之旅------>对Android开发者有益的40条优化建议

    下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...

  4. Spring 拦截器——HandlerInterceptor

    采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...

  5. xcode6

    官方的xcode6下载太慢,这里送上百度网盘地址: http://pan.baidu.com/s/1hqze1hi

  6. Android JSON And Object Cast

    Ref:JSON字符串转换成Java实体类(POJO) Ref:Java.Json转换方式之二:Jackson Ref:Jackson 框架,轻易转换JSON Ref:几种序列化协议(protobuf ...

  7. requests ip代理池单ip和多ip设置方式

    reqeusts库,在使用ip代理时,单ip代理和多ip代理的写法不同 (目前测试通过,如有错误,请评论指正) 单ip代理模式 省去headers等 import requests proxy = { ...

  8. ubuntu切换到root

    sudo+命令,输入当前用户密码后以root权限执行命令,有时间限制且仅限当前命令. sudo -i,输入当前用户密码后以root权限登录shell,无时间限制.使用exit或logout退出. su ...

  9. 小程序 requestAnimationFrame 死循环

    小程序没有requestAnimationFrame  这个方法,小游戏有,使用这个方法会造成死循环

  10. 培训笔记——ubuntu安装

    1.选择安装位置,如果是做双系统提前准备一个分区,如果覆盖安装就无所谓了2.下载iso镜像文件,制作启动盘,Windows或linux环境下分别有相应的软件可以制作启动光盘或U盘3.开始安装一 设置开 ...