Uva Galaxy (Ruler extraction)

Source: Internet
Author: User

Test instructions: n planets, given a one-dimensional coordinate of each point, can delete m points, so that all remaining points to the center of the remaining points of the sum of the square and the smallest, to find the minimum value;

Idea: Ruler method, each maintenance n-m point; Long Long is read in with%LLD, but codeblocks compile will cause problems, so use cin>>;

#include <cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>using namespacestd;intt,n,m;Long Longs1,s2;Long Longnum[50500];Doublesum,mid,cnt;intMain () {intI,j,k,l,r; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m);  for(i=1; i<=n;i++) {scanf ("%lld",&Num[i]); }      if(n==m) {printf ("0\n"); Continue; } sort (num+1, num+n+1); S1=0; s2=0; cnt=0;  for(i=1; i<=n-m;i++) {S1+=Num[i]; S2+=num[i]*Num[i]; } Mid=s1*1.0/(nm); CNT=s2+ (n-m) *mid*mid-2*mid*S1;  for(i=1; i<=m;i++) {S1=s1-num[i]+num[n-m+i]; S2=s2-num[i]*num[i]+num[n-m+i]*num[n-m+i]; Mid=s1*1.0/(nm); Doubletemp=s2+ (n-m) *mid*mid-2*mid*S1; if(temp<CNT) {CNT=temp; }} printf ("%.10f\n", CNT); }    return 0;}

Uva Galaxy (Ruler extraction)

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.