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);
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值