HDU 5073 Galaxy
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 rock Galaxy. there are n stars in neogalaxy, 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 enough t 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 w I is the weight of star I, d I is the distance form star I to the mass of center.
As DRD has ipvs 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 transpZ networks · networks "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> latency + latency = "pst"> Input
The first line contains an integer T (T Ü U 10), denoting the number of the test cases.
For each test case, the first line contains two integers, n (1 Ü U n Ü U 50000) and k (0 ¡U k ¡U 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
23 2-1 0 14 2-2 -1 1 2
Sample Output
00.5
Ì â Ò â £° ø ää 2 ö öê ý n, m n'ú±íó Ð n ö öµ£ £20.m 'ú±íäülüräm' Î Ò Æ µ µä² Ù × £ ;;********************************************************* μ Ä µä¾ à ë Æ ° ½ · ½ ° × î Ð ¡
~~ · £° ¿'juanyant=£°n-k? £çø ¼ ä£;%%%^ö× %%%%%%%µä
Ac'úâ £°
#include
#include
#include#include
using namespace std;#define N 50000+5double a[N],b[N];int main(){ int n,k; int i,j; int t; double mid; double s1,s2; scanf("%d",&t); while(t--) { double sum=0; scanf("%d %d",&n,&k); for(i=1;i<=n;i++) scanf("%d",&a[i]); if(n!=k) { sort(a+1,a+n+1); for(i=1;i<=n;i++) b[i]=a[i]*a[i]; s1=s2=0; for(i=1;i<=n-k;i++) { s1+=a[i]; s2+=b[i]; } sum=s2-s1*s1/(n-k); for(i=2,j=n-k+1;j<=n;i++,j++) { s1-=a[i-1]; s1+=a[j]; s2-=b[i-1]; s2+=b[j]; double t=s2-s1*s1/(n-k); if(t