程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 5073 Galaxy(居然是暴力)

HDU 5073 Galaxy(居然是暴力)

編輯:C++入門知識

HDU 5073 Galaxy(居然是暴力)


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 fZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcm11bGE8YnI+Cjxicj4KPGNlbnRlcj48aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20141024/2014102408595938.jpg" alt="\">
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

題意:

給你n個點,排序後可以刪除m個點,然後找一個點(可以不是這n個點),求所有點到這個點的距離的平方和


思路:暴力枚舉左邊刪除幾個點,右邊刪除幾個點,然後求出所求取優

但是怎麼求所求呢?假設那個點為t,那麼就是(x1-t)^2+(x2-t)^2+.....(xn-t)^2

我這裡取n為3時方便 就是x1*x1 +x2*x2 +x3*x3 -2(x1+x2+x3)*t+3*t*t,此時最優的答案 t 是(x1+x2+x3)/3 (注意題目是n個點刪除m個,所以程序中應把3換成 n-m )

然後把t帶入就可以了


詳情看代碼,不懂就看上面的式子


#include
#include
#include
#include

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define INF 0x3f3f3f3f
#define N  50005

double a[N],sum[N],temp[N];
int n,m;

double fdd(int s,int e) //s是左邊刪除的點個數,e是右邊刪除的點個數
{
    int i,j;
	s=1+s;   //算出左起點
	e=n-e;   //算出右終點
	double t=(temp[e]-temp[s-1])/(n-m);
	return sum[e]-sum[s-1]-2*(temp[e]-temp[s-1])*t+(n-m)*t*t; //不懂看上面的式子
}

int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		sum[0]=temp[0]=0;

		for(i=1;i<=n;i++)
		{
			scanf("%lf",&a[i]);
		}

       sort(a+1,a+n+1);
       for(i=1;i<=n;i++)
	   {
	   	  sum[i]=sum[i-1]+a[i]*a[i];
		  temp[i]=temp[i-1]+a[i];
	   }

		double ans=fdd(0,m);

		if(n-m<=1)
		{
			printf("0.0000000000\n");
			continue;
		}

		for(i=0;i<=m;i++)
			ans=min(ans,fdd(i,m-i));

		printf("%.10f\n",ans);
	}
    return 0;
}







  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved