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)