HDOJ Galaxy 5073【2014年鞍山区域赛D题-方差】

Galaxy

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

Total Submission(s): 2800    Accepted Submission(s): 684

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

2014 Asia AnShan Regional Contest

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  5342 5341 5340 5339 5338

求方差~

讲解:点击打开链接

输入格式后面的0不用去掉,题目没要求

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 50000+10

using namespace std;

double s[maxn];

int main()
{
	int t;
	scanf("%d",&t);
	int n,k;
	while(t--){
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++){
			scanf("%lf",&s[i]);
		}
		sort(s,s+n);
		int cnt=n-k;
		if(n==k){
			printf("0\n");
			continue;
		}
		double sum1=0;
		double sum2=0;
		for(int i=0;i<cnt;i++){
			sum1+=s[i];
			sum2+=s[i]*s[i];
		}
		double ans=sum2-(sum1*sum1)/cnt;
		for(int i=cnt;i<n;i++){
			sum1+=s[i];
			sum2+=s[i]*s[i];
			sum1-=s[i-cnt];
			sum2-=s[i-cnt]*s[i-cnt];
			ans=min(ans,(sum2-(sum1*sum1/(n-k))));
		}
		printf("%.10lf\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-08-29 23:26:35

HDOJ Galaxy 5073【2014年鞍山区域赛D题-方差】的相关文章

hdu5080:几何+polya计数(鞍山区域赛K题)

/* 鞍山区域赛的K题..当时比赛都没来得及看(反正看了也不会) 学了polya定理之后就赶紧跑来补这个题.. 由于几何比较烂写了又丑又长的代码,还debug了很久.. 比较感动的是竟然1Y了.. */ 题目大意: 给定一些点,某些点上有边,问用k种颜色染色的等价类有多少种 思路: 由于坐标是整数..只有可能旋转90,180,270才能得到置换 且图形必须为中心对称图形 先用几何方法找出对称中心 然后旋转,找是否出现置换... 由于点数只有50,几何预处理这一部分可以很暴力无脑的写..各种判断相

HDOJ Osu! 5078【2014鞍山区域赛I题-水】

Osu! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1263    Accepted Submission(s): 660 Special Judge Problem Description Osu! is a very popular music game. Basically, it is a game about cli

HDU-5001 Walk 2014年鞍山网络赛E题

依次枚举每个不能走过的点,DP递推下一步情况,求出所有其他点的概率之和即为这个点不会被走过的概率. #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <iomanip> #include

zoj 3829 Known Notation(2014年牡丹江区域赛k题)

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre

[暑假集训]区域赛套题集

2014-07-03 [浙江第11届省赛]ZOJ 3785 What day is that day?  (打表找循环节) [暑假集训]区域赛套题集

Heshen&#39;s Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)

Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole country's wealth by corruption. So he was known as the most corrupt official in Chinese history. But Emperor Qianlong liked, or even loved him so much

2017年ICPC中国大陆区域赛真题(下)

2017年ICPC中国大陆区域赛真题(下) A - Lovers #include <bits/stdc++.h> using namespace std; const int maxn=2e5+10; int n,k,a[maxn],b[maxn],ans; int main() { int _; scanf("%d", &_); while (_--) { scanf("%d%d", &n, &k); for (int i =

鞍山区域赛的总结+反思

玩acm大概也有半年了吧,大概是从大一下学期中后段的那一段时间开始接触acm的,暑假参加了集训之后,就开始了区域赛的网络赛的选拔,哎弱校没人权,最终组好的队伍也就那么四五队,弱校的acm氛围简直太差了,无法吸引很多的学生一起来玩耍,网络赛选拔的时候我们队是零贡献的,因为,有些师兄队要和我们共用一个账号,那些水题往往被他们很快先ac掉了,难题我们又做不了,只能让师兄们去做,多亏了他们,让scnu的每个区域赛都获得了现场赛的名额.哎 牡丹江凑不齐人数,结果最终只能放弃这个赛区了.两个字,弱校----

2014 acm亚洲区域赛(北京)总结

2014 acm/icpc 亚洲区域赛北京站    第一次到外面去比赛,周五下午做高铁到北京,报到注册后就去找在北京的同学玩了.    周六,上午,随便拉了个cf,我们随便看了下,没有写代码.下午的开幕式就那样,几个领导讲几句话,之后的热身赛,A题是数试题集出现多少次ACM,我们理解为在这题出现了多少次?第一次交了个17,wa掉了,交了个19,又wa了,后来好像又交了个13,都wa掉了,想哭了%>_<%,后面的D题更逗,给一段代码,判断是wa还是TLE,只有一个测试文件,并且样例数<=3