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. 封装一个ViewPager真正的实现图片无限循环滚动带导航点

    效果图: 大家在写项目的过程中常常会碰到须要实现Viewpager里面载入几张图片来循环自己主动轮播的效果,假设不封装一下的话代码分散在activity里面会显得非常乱.并且也不利于我们下次复用,所以 ...

  2. less06 引入(importing)

    main.less @wp:960px; .colorsss{ color: darkgreen; } index.css .color{ color: #ff6600; } style.less / ...

  3. React ----- 路由懒加载的几种实现方案

    传统的两种方式 1.import() 符合ECMAScript提议的import()语法,该提案与普通 import 语句或 require 函数的类似,但返回一个 Promise 对象.这意味着模块 ...

  4. String methods

    A method is similar to a function – it takes arguments and returns a value – but the syntax is diffe ...

  5. 用jquery控制表格奇偶行及活动行颜色

    虽然jquery流行已经很多年了,一直都感觉很难,也没有花时间去学习它,只是偶尔哪天心血来潮了去看一点点,时隔多日又会忘得一干二净.最近用到表格奇偶行不同色,不得不去再看jquery,虽然感觉还是难, ...

  6. H5操作WebSQL数据库

    HTML代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  7. Vue和vue-template-compiler版本之间的问题

    今天把远程仓库拉下项目,运行'npm run dev'时,报错 Module build failed: Error: Cannot find module 'vue-template-compile ...

  8. python 异步IO-aiohttp与简单的异步HTTP客户端/服务器

    参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143209814 ...

  9. 【习题 8-16 UVA - 1618】Weak Key

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举N[q]和N[r]的位置 因为N[q]是最大值,且N[r]是最小值. 且它们是中间的两个. 枚举这两个可以做到不重复枚举. 然后 ...

  10. Spring Cloud学习笔记【九】配置中心Spring Cloud Config

    Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...