Hdu 5073 Galaxy 精度问题

  思路: 其实求解很简单直接说解法,移动K个后 上下的角动量最小,能肯定是相连的(n-k)个,至于为什么 你自己好好想想(easy);

     对于一些等质量的质点中心在 所在位置和除以点的个数

     average=sum[l,l+(n-k)-1]/(n-k);

      一个点的值: (pi-average)* (pi-average)

      也就是 pi^2+avery^2 - 2*pi*average

      多个点相加也就是 ∑pi^2+(n-k)*sum*sum/(n-k)/(n-k)   -  2*∑pi*sum/(n-k);

            =   ∑pi^2+(n-k)*sum*sum/(n-k)/(n-k)   -  2*sum*sum/(n-k);

            =   ∑pi^2   -  sum*sum/(n-k);

     所以要处理一下 ”普通和“,“平方和” 就好了

     但是这个卡精度:我的做法  每次比较   (n-k)*∑pi^2   -  sum*sum

     剩下最小的 再除以(n-k) 就好了

代码.cpp

 1 #include <string>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <cstdio>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 int n,k,d[50005];
 9 ll sum[50005];
10 ll getsum(int i,int j)
11 {
12     if(i)return sum[j-1]-sum[i-1];
13     else return sum[j-1];
14 }
15
16 int main()
17 {
18     int t;
19     scanf("%d",&t);
20     while(t--)
21     {
22         memset(d,0,sizeof d);
23         memset(sum,0,sizeof sum);
24         scanf("%d%d",&n,&k);
25         for(int i=0;i<n;i++)
26             scanf("%d",&d[i]);
27         sort(d,d+n);
28         for(int i=0;i<n;i++)
29             if(i) sum[i]=sum[i-1]+(ll)d[i];
30             else sum[i]=(ll)d[i];
31         double messc=0.0,minv=0.0,now=0.0;
32         for(int j=0;j<=k;j++)
33         {
34             ll nows=getsum(j,n-(k-j));
35             messc=(double)nows/(double)(n-k);
36             now=0.0;
37             for(int i=j;i<=(n-1)-(k-j);i++)
38             {
39                 double dis=(double)d[i]-messc;
40                 now+=(dis*dis);
41                 if(j && now>=minv) break;
42             }
43             if(!j)minv=now;
44             else if(now<minv) minv=now;
45         }
46         printf("%.12f\n",minv);
47     }
48     return 0;
49 }
时间: 2024-09-30 12:17:48

Hdu 5073 Galaxy 精度问题的相关文章

hdu 5073 Galaxy(数学)

题目链接:hdu 5073 Galaxy 题目大意:给定N个点,可以移动其中的K的点,问说最后I的最小值可以是多少. 解题思路:因为质量都为1嘛,所以就是求方差,可以移动K个,所以即选连续的n=N-K个使得方差最小.注意N=K的情 况. S表示n个数的和,T表示n个数平方的和,那么这n个数的方差即为T - S * S / n,然后扫描一遍数组维护S,T,并且计算 方差的最小值. #include <cstdio> #include <cstring> #include <al

HDU 5073 Galaxy(贪心)

题目链接:HDU 5073 Galaxy 题面: Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2571    Accepted Submission(s): 642 Special Judge Problem Description Good news for us: to release the financia

HDU 5073 Galaxy (2014鞍山现场赛D题)

题目链接:HDU 5073 Galaxy 题意:在一维的坐标系里,给出N个点坐标,转动K个点,使转动后这个星系的的惯性最小(根据题意惯性最小也就是 求所有星星到星系中心的距离最小,这个可以理解成方差最小).求最小的惯性. 思路: 先对序列排序,再求出算N-K个点惯性的递推式. 以三个为例: 预处理是 平均数和各项的平方和, 注意:n==k的特判 AC代码: #include <stdio.h> #include <algorithm> using namespace std; do

hdu 5073 Galaxy(2014acm亚洲赛区鞍山 D)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 768    Accepted Submission(s): 179 Special Judge Problem Description Good news for u

hdu 5073 Galaxy(2014acm亚洲赛区鞍山 C)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 768    Accepted Submission(s): 179 Special Judge Problem Description Good news for u

hdu 5073 Galaxy (数学+推导)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 553    Accepted Submission(s): 126 Special Judge Problem Description Good news for us: to release the financial pressure, the government

HDU 5073 Galaxy (数学)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 4991    Accepted Submission(s): 1215Special Judge Problem Description Good news for us: to release the financial pressure, the government

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 fashi

ACM学习历程—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,