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 ...
随机推荐
- bzoj1012: [JSOI2008]最大数maxnumber(貌似是道线段树喔)
1012: [JSOI2008]最大数maxnumber 题目:传送门 题解: 发现自己空了一道水题... 1~210000建线段树,其实就是一道裸题... 单点修改+区间查询...1A~ 代码: # ...
- PHPStorm打开文件所在目录
很实用~
- rest_framework 解析器(下 全局配置使用)
解析器 一般都是全局设置 参考文档 www.cnblogs.com/wupeiqi/articles/.html REST_FRAMEWORK=( "DEFAULT_PARSER_CLASS ...
- HTML 表格 做个人简历
根据老师上课讲的常用标签与表格的应用 终于做出了第一个网页版的个人简历 虽然作出来了 但是感觉其中方法有点儿问题 还需要进一步的改进中…… <!DOCTYPE html PUBLIC " ...
- 加载等待loading
自己写的一个小插件,还有很多需要完善... (function ($) { $.fn.StartLoading = function (option) { var defaultV ...
- caffe(4) 激活层(Activation Layers)及参数
在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...
- Android向unity发送消息
有些时候需要Android向unity发送消息,有两种方法实现,一.通过unity再带的消息机制,二.通过注册回调的方式. 一.通过UnityPlayer.UnitySendMessage():方法 ...
- 【Henu ACM Round#20 B】Contest
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 根据时间和原分数. 算出对应的分数就可以了. [代码] #include <bits/stdc++.h> using n ...
- LINUX 系统初始化脚本
#!/bin/bash ######the system first start configuretion #####for install ####copy right by donglei## ...
- LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
一.题目 Construct Binary Tree from Inorder and Postorder Traversal My Submissions Given inorder and pos ...