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夫妇也退居了二线 ...
随机推荐
- CF:Problem 426B - Sereja and Mirroring 二分或者分治
这题解法怎么说呢,由于我是把行数逐步除以2暴力得到的答案,所以有点二分的意思,可是昨天琦神说是有点像分治的意思.反正总的来说:就是从大逐步细化找到最优答案. 可是昨晚傻B了.靠! 多写了点东西,然后就 ...
- [T-SQL] 获取拼音
)) ) as begin ) ) declare @i int declare @words_len int declare @unicode int set @words = ltrim(rtri ...
- 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双
[BZOJ3331][BeiJing2013]压力 Description 如今,路由器和交换机构建起了互联网的骨架.处在互联网的骨干位置的核心路由器典型的要处理100Gbit/s的网络流量.他们每天 ...
- 虚拟化构建二分图(BZOJ2080 题解+浅谈几道双栈排序思想的题)
虚拟化构建二分图 ------BZOJ2080 题解+浅谈几道双栈排序思想的题 本题的题解在最下面↓↓↓ 不得不说,第一次接触类似于双栈排序的这种题,是在BZOJ的五月月赛上. [BZOJ4881][ ...
- 第一章 MATLAB数字图像处理编程基础
1 为什么用MATLAB MATLAB的图像处理工具箱(Image Processing Toolbox,IPT)封装了一系列不同图像处理需求的标准算法,它们都是通过直接或间接调用MATLAB中矩阵运 ...
- 【题解】P2161[SHOI2009]会场预约(set)
[题解][P2161 SHOI2009]会场预约 题目很像[[题解]APIO2009]会议中心 \(set\)大法好啊! 然后我们有个小\(trick\)(炒鸡帅),就是如何优雅地判断线段交? str ...
- require.js vs browserify
require.js vs browserify require.js是模块加载器:browserify是预编译工具 require.js遵循的是AMD规范:browserify遵循的是CommonJ ...
- Jquery定义对象( 闭包)
转自:http://www.cnblogs.com/springsnow/archive/2010/06/03/1750832.html 例一:添加对象的静态属性 声明一个对象$.problemWo, ...
- 淘宝开源平台(taobao-code)使用
偶尔之下翻到的这个东西,瞬间觉得足以解决自己在开发过程中的版本控制问题.就注册了一个试试.先是在度娘上搜寻“淘code”,进入官网之后直接注册.然后构建自己的项目,上传代码就OK了. 一.搜寻“淘co ...
- windows中检查端口占用
在cmd中怎么输入netstat -aon|findstr "9080" 返回: UDP 0.0.0.0:8001 *.* 其中的4220为进城PID