CCPC/ACM(杭州)熱身賽

There are some positive integer numbers, and you want to divide them into two nonempty sets (every integer should be in and only in one set) and get a value descripted below:

Assume that set AA has n1n1 numbers A1,A2,⋯,An1A1,A2,⋯,An1; set BB has n2n2 numbers B1,B2,⋯,Bn2B1,B2,⋯,Bn2.
Let

v1=∑i=1n1Ain1
v1=∑i=1n1Ain1

v2=∑i=1n2Bin2
v2=∑i=1n2Bin2
Then

ans=∑i=1n1(Ai−v2)2+∑i=1n2(Bi−v1)2
ans=∑i=1n1(Ai−v2)2+∑i=1n2(Bi−v1)2
We want to know the largest ans.

Input
The first line of the input is an integer TT (T≤20T≤20), which stands for the number of test cases you need to solve.

Every test case begins with an integer NN (2≤N≤10002≤N≤1000), then followed by NN positive integers on the next line, these numbers will not exceed 10001000.

Output
For every test case, you should output Case #k: first, where kk indicates the case number and starts at 11. Then output the answer rounded to six digits after the decimal point. See sample for more details.

Sample Input
2
3
1 1 1
3
1 2 3

Sample Output
Case #1: 0.000000
Case #2: 7.250000
大致就是排個序,然後枚舉下斷點就行了。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
#define F(x,a,b) for (int x=a;x<=b;x++)
int a[10005],b[10005];
int main()
{
   int T;
   scanf("%d",&T);

   F(kk,1,T){
       int n;
       scanf("%d",&n);
       F(i,1,n) scanf("%d",&a[i]);
       sort(a+1,a+n+1);
       memset(b,0,sizeof(b));
       b[1]=a[1];
       F(i,1,n-1) b[i+1]=b[i]+a[i+1];
       double ans=0;
       double maxx=-1;
       double v1,v2;
       F(i,2,n)
       {
           v1=(b[i-1]-b[0])/((i-1)*1.0);
           v2=(b[n]-b[i-1])/((n-i+1)*1.0);
           ans=0;
           F(j,1,i-1)
           {
               ans+=(a[j]-v2)*(a[j]-v2);
           }
           F(j,i,n)
           {
               ans+=(a[j]-v1)*(a[j]-v1);
           }
           if (ans>maxx) maxx=ans;
        }
        printf("Case #%d: %.6lf\n",kk,maxx);
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章