hdu 5073 Galaxy(2014 鞍山现场赛)
Galaxy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144
K (Java/Others)
Total Submission(s): 577 Accepted Submission(s): 132
Special Judge

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 wi is the weight of star i, di 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.
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.
2
3 2
-1 0 1
4 2
-2 -1 1 2
0
0.5
(xi - x平均)^2 = xi^2 + x平均^2 - 2*xi*x平均
所以方差 = ∑xi^2 + n*x平均 - 2*∑xi*x平均
设v=n-k,先计算前v个的∑xi^2,算出平均值,就能够算出方差,然后每次向后处理一次,减去前面的一个,加上
后一个,算出平均值,再求出方差,这样就能够在O(n)出结果了。
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
long long a[50005];
using namespace std; int main()
{
int t,n,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i = 1;i<=n;i++)
cin>>a[i];
if(n==k)
{
printf("0\n");
continue;
}
sort(a+1,a+n+1);
long long sum = 0;
long long sums = 0;
for(int i = 1;i<=n-k;i++)
{
sum += a[i];
sums += a[i] * a[i];
}
double ave = sum*1.0/(n-k);
double mini = sums + (n-k)*ave*ave - 2*sum*ave;
for(int i = 1 ;i <= k; i++)
{
sum = sum - a[i]+a[n-k+i];
sums = sums - a[i]*a[i]+ a[n-k+i]*a[n-k+i];
ave = sum*1.0/(n-k);
double temp = sums + (n-k)*ave*ave - 2*sum*ave;
if(temp < mini)
{
mini = temp;
}
}
printf("%.10f\n",mini);
}
}
ps:cdd用了一个错误的贪心策略,居然也能A,还没特判n==k。这真是一个看脸的世界o(╯□╰)o
hdu 5073 Galaxy(2014 鞍山现场赛)的更多相关文章
- HDU 5073 Galaxy(2014鞍山赛区现场赛D题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...
- HDU 5073 Galaxy (2014 Anshan D简单数学)
HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...
- hdu 5078 2014鞍山现场赛 水题
http://acm.hdu.edu.cn/showproblem.php?pid=5078 现场最水的一道题 连排序都不用,由于说了ti<ti+1 //#pragma comment(link ...
- hdu 5071(2014鞍山现场赛B题,大模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...
- hdu 5078(2014鞍山现场赛 I题)
数据 表示每次到达某个位置的坐标和时间 计算出每对相邻点之间转移的速度(两点间距离距离/相隔时间) 输出最大值 Sample Input252 1 9//t x y3 7 25 9 06 6 37 6 ...
- 2014鞍山现场赛C题HDU5072(素筛+容斥原理)
Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- hdu 5122 (2014北京现场赛 K题)
把一个序列按从小到大排序 要执行多少次操作 只需要从右往左统计,并且不断更新最小值,若当前数为最小值,则将最小值更新为当前数,否则sum+1 Sample Input255 4 3 2 155 1 2 ...
- HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题
推公式 #include <cstdio> #include <cmath> #include <iomanip> #include <iostream> ...
- hdu 5078 Osu!(鞍山现场赛)
Osu! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Sub ...
随机推荐
- ubuntu 非长期支持版升级系统版本号(ssh登录情况适用)
(1)当前系统为非长期支持版.而且已被废弃,仅仅能逐版本号升级 以当前系统版本号为11.10为例 改动source.list更新源为通用old源,由于原来的源已经不可用 deb http://old- ...
- ASMlib操作系统包安装与配置asm disk磁盘
1.加入6块硬盘,每块100g.不管是热加还是冷加.不管是加硬盘,用san存储划lun,或者再加上多路径,都是能够这么做的. 在操作系统层,能识别这种lun.以下的sdb就是一个刚划分的300g的lu ...
- Java中泛型的各种使用
Java中的泛型的使用: 1.普通的泛型使用 在使用类的时候后面的<>中的类型就是我们确定的类型. public class MyClass1<T> {//此处定义的泛型是T ...
- zzulioj--1804--ZY学长的密码(字符串)
1804: ZY学长的密码 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 140 Solved: 53 SubmitStatusWeb Board ...
- Kali linux 2016.2(Rolling)里Metasploit的数据库
为什么要在Metasploit里提及到数据库? 大家都知道,这么多信息,我怎样才能把它们整理好并保存起来?怎么展现给老大看,最后怎么体现在要提交的渗透测试报告中呢? 你的担忧真的很有必要,因为啊, ...
- 自动关闭Messbox
/// <summary> /// 自动关闭Messbox /// </summary> public class MessageBoxAutoClose { System.T ...
- UVa 1001 Say Cheese【floyd】
题意:在一个三维的奶酪里面有n(n<=100)个洞,老鼠A想到达老鼠B的位置, 在洞里面可以瞬间移动,在洞外面的移动速度为10秒一个单位,求最短时间 看到n<=100,又是求最短时间,想到 ...
- XRDP与VNC的关系(转载)
XRDP与VNC的关系 如果仅仅安装XRDP协议.是不能在windows上使用远程桌面连接到Ubuntu. 还须要安装VNCServer才行. 所以,XRDP启动之后.系统会自己主动启动一个VNC会话 ...
- golang实现php里的serialize()和unserialize()序列和反序列方法
Golang 实现 PHP里的 serialize() . unserialize() 安装 go get -u github.com/techleeone/gophp/serialize 用法 pa ...
- 题解 P2330 【[SCOI2005]繁忙的都市】
又是一道Kruskal题目. AC代码见下. 主要思路就是将所有的边储存起来,然后进行贪心地选择,期间需要判断两个端点是否有关联,这一过程通过并查集实现.Kruskal部分套模板就可以了. #incl ...