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 = ;
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(数学)的更多相关文章
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5587 Array(数学 && 二分 && 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...
- ACM学习历程—HDU1030 Delta-wave(数学)
Description A triangle field is numbered with successive integers in the way shown on the picture be ...
- ACM学习历程—HDU 5317 RGCDQ (数论)
Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...
- ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...
随机推荐
- 安装部署服务器和javaweb项目
[说明]总算告一段落了,服务器啊服务器,你可是把我折磨的够呛,不过现在的状态我已经很满足了. [说明]下面的图片是我这两天一直在搞的,内容不能说是重复,只能说是不停地修改修改,出错出错. 1) 虚拟主 ...
- Java编码规范之数据对象命名
数据对象分多种,为方便阅读并区分各数据对象的用途,习惯将数据对象分为以下几类,供参考: 持久对象 PO(persistant object)对象关系映射(ORM)概念的产物,基本上对象的成员变量对应了 ...
- 九度OJ 1208:10进制 VS 2进制 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2040 解决:612 题目描述: 对于一个十进制数A,将A转换为二进制数,然后按位逆序排列,再转换为十进制数B,我们乘B为A的二进制逆序数. ...
- mybatis generator的用法
1 自动生成代码 配置数据库 自动生成三个文件: 第一,java bean文件: 第二,java bean对应的dao文件,但是这里的dao只是一个接口: 第三,mybatis需要的Mapper文件: ...
- ASP连接数据库SQLServer
Set conn=Server.CreateObject("adodb.connection")Set conn1=Server.CreateObject("adodb. ...
- HTML/CSS/JS初始化
CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...
- 我的Android进阶之旅------>Android SDK支持的配置标识符(有用的参考文件)
Android SDK支持的配置标致符 配置标识符 标识符值 描 述 MCC MNC 例子: mcc310: MCC310-MNC004: MCC208-MNC00 MCC(移动国家代码 ...
- ABAP下载xml文件
[转http://www.cnblogs.com/byfhd/archive/2007/08/17/859829.html] ************************************* ...
- onActivityResult方法不执行,什么原因?
原因: public void OnOpenPlayersActivity(View view) { Intent intent = new Intent(); intent.setClass(g ...
- client = new DatagramSocket(LocalPort) 是说端口已经被占用的意思
ok 现在遇到一个问题,client = new DatagramSocket(LocalPort) 是说端口已经被占用的意思 ref:!!https://community.oracle.com/t ...