HDU 5073 galaxy Accuracy

Source: Internet
Author: User

 

Idea: in fact, the solution is very simple. If we move K numbers and then the upper and lower degrees have the smallest angular momentum, it must be connected (n-k). As for why do you Think About It (easy );

For some mass point centers in the location and the number of points divided

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

Value of one vertex: (Pi-average) * (Pi-average)

That is, PI ^ 2 + Avery ^ 2-2 * pI * Average

Add multiple vertices, that is, Σ PI ^ 2 + (n-k) * sum/(n-k) -2 * Σ pI * sum/(n-k );

= Σ PI ^ 2 + (n-k) * sum/(n-k)-2 * sum/(n-k );

= Σ PI ^ 2-sum * sum/(n-k );

So we need to deal with "common" and "sum of squares ".

However, this card accuracy: My practice is to compare (n-k) * Σ PI ^ 2-sum * sum

Just divide the smallest value by (n-k ).

Code. 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 }

 

HDU 5073 galaxy Accuracy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.