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

Problem 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 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.
 
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
 
Source
 


    平方差公式展开一下就能够了。

   (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 鞍山现场赛)的更多相关文章

  1. HDU 5073 Galaxy(2014鞍山赛区现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...

  2. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  3. hdu 5078 2014鞍山现场赛 水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5078 现场最水的一道题 连排序都不用,由于说了ti<ti+1 //#pragma comment(link ...

  4. hdu 5071(2014鞍山现场赛B题,大模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...

  5. hdu 5078(2014鞍山现场赛 I题)

    数据 表示每次到达某个位置的坐标和时间 计算出每对相邻点之间转移的速度(两点间距离距离/相隔时间) 输出最大值 Sample Input252 1 9//t x y3 7 25 9 06 6 37 6 ...

  6. 2014鞍山现场赛C题HDU5072(素筛+容斥原理)

    Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  7. hdu 5122 (2014北京现场赛 K题)

    把一个序列按从小到大排序 要执行多少次操作 只需要从右往左统计,并且不断更新最小值,若当前数为最小值,则将最小值更新为当前数,否则sum+1 Sample Input255 4 3 2 155 1 2 ...

  8. HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题

    推公式 #include <cstdio> #include <cmath> #include <iomanip> #include <iostream> ...

  9. hdu 5078 Osu!(鞍山现场赛)

    Osu! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Sub ...

随机推荐

  1. bzoj1051: [HAOI2006]受欢迎的牛(强联通)

    1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...

  2. zookeeper伪分布安装配置

    1.下载路径为:http://mirrors.cnnic.cn/apache/zookeeper/stable/ 2.安装: 第一步 解压zookeeper压缩包: 进入 zookeeper安装目录 ...

  3. java9新特性-15-全新的HTTP 客户端API

    1.官方Feature 110: HTTP 2 Client 2.使用说明 HTTP,用于传输网页的协议,早在1997年就被采用在目前的1.1版本中.直到2015年,HTTP2才成为标准.     H ...

  4. java9新特性-5-Java的REPL工具: jShell命令

    1.官方Feature 222: jshell: The Java Shell (Read-Eval-Print Loop) 2.产生背景 像Python 和 Scala 之类的语言早就有交互式编程环 ...

  5. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  6. Android chromium 2

    Overview JNI (Java Native Interface) is the mechanism that enables Java code to call native function ...

  7. sysctl---内核参数相关设置

    sysctl命令被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中.它包含一些TCP/ip堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能 ...

  8. 人在IT,关于计算机专业的杂谈PPT

  9. win下通过pip安装TensorFlow

    官方介绍(超详细):https://www.tensorflow.org/install/pip 按照官方介绍,不同的TensorFlow版本只支持特定的python版本所以你要是下载.whl包安装的 ...

  10. 浅析[分块]qwq

    首先说明这篇博客写得奇差无比 让我们理清一下为什么要打分块,在大部分情况下,线段树啊,splay,treap,主席树什么的都要比分块的效率高得多,但是在出问题的时候如果你和这些数据结构只是混的脸熟的话 ...