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 ...
随机推荐
- Android Camera+SurfaceView实现自己定义拍照
对Activity强制横屏,保证预览方向正确. 使用OrientationEventListener监听设备方向.推断竖拍时,旋转照片后再保存.保证竖拍时预览图片和保存后的图片方向一致. 执行效果: ...
- Android sdk版本以及兼容性问题
Android:minSdkVersion —— 此属性决定你的应用能兼容的最低的系统版本,一盘情况是必须设置此属性. android:targetSdkVersion —— 此属性说明你当前的应用是 ...
- bzoj2127: happiness(双倍经验最小割)
2127: happiness 题目:传送门 题解: 双倍经验美滋滋~ 请看蒟蒻以前写的渣题解...bzoj3894 表示做完自己就最小割了... 代码(直接改的...菜啊): #include< ...
- 最短路径----SPFA算法
求最短路径的算法有许多种,除了排序外,恐怕是ACM界中解决同一类问题算法最多的了.最熟悉的无疑是Dijkstra,接着是Bellman-Ford,它们都可以求出由一个源点向其他各点的最短路径:如果我们 ...
- [luogu P3360] 偷天换日 解题报告(树形DP)
题目链接:https://www.luogu.org/problemnew/show/P3360 题解: 首先我们把边上的消耗放到向下的点上,如果是叶子节点的话就先做一次0/1背包 发现这是一颗二叉树 ...
- [AGC018 B] Sports Festival 解题报告
题目链接:https://agc018.contest.atcoder.jp/tasks/agc018_b 题目: 题目大意: 有N个人参加M个体育项目,每个人对体育项目的喜爱程度有一个排名,A[i] ...
- [转]Adobe Creative Cloud 2015 下载 Adobe CC 2015 Download
Adobe Creative Cloud 2015 下载 Adobe 宣布 Creative Cloud 设计套件全线更新! Adobe CC 2015新功能包括: – Premiere Pro ...
- tomcat web容器工作原理
Tomcat的模块结构设计的相当好,而且其Web 容器的性能相当出色.JBoss直接就使用了Tomcat的web容器,WebLogic的早期版本也是使用了Tomcat的代码.Web容器的工作过程在下面 ...
- bzoj1922 [SDOI2010]大陆争霸 分层图
问题描述 幻想历8012年5月12日深夜,斯普林·布拉泽降下神谕:“Trust me, earn eternal life.”克里斯军团士气大增.作为克里斯军团的主帅,你决定利用这一机会发动奇袭,一举 ...
- memset函数的用法
重点需要注意一个细节:memset是以byte来填充的,也就是一个字节(8bit)一个单位来填充. 因此:如果int array[10]: memset(array, 1, 10):然后再把array ...