Scapegoat Gym - 101775B 思维题

Aori is very careless so she is always making troubles. One day she does it again, with N big troubles! But this time she seems to be at ease because she has found M Inklings to take all the blames. Each trouble can be measured by a severity number ai. Each trouble needs at least one Inkling to take the blame, and each Inkling can help Aori to take the blame for exactly one trouble. If two or more Inklings take the same trouble, they will take this blame together and discuss how to divide this trouble into.. some trivial things.. to reduce the pressure on each Inkling, as long as the total severity on Inklings is equal to the severity of this trouble.

Inklings are so warm so that Aori wants to think a way to let the variance of severity on each Inkling to be minimal. Could you help Aori make her scapegoats?

Formally, the variance of variables is the expectation of the squared deviation of a variable from its mean:

Input

The first line of the input gives the number of test cases, T. T test cases follow.

For each test case, the first line contains two integers N and M, where N is the number of troubles, and M is the number of Inklings. The next line contains N integers a1, a2, ..., aN representing the severity of the troubles that Aori makes.

  • 1 ≤ T ≤ 100.
  • 1 ≤ N ≤ M ≤ 2 × 105.
  • 1 ≤ ai ≤ 10000.
  • .

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimal variance.

y will be considered correct if it is within an absolute or relative error of 10 - 8 of the correct answer.

Example

Input

3
3 6
1 2 3
5 10
5 6 7 8 9
6 6
1 1 4 5 1 4

Output

Case #1: 0.000000000000
Case #2: 0.400000000000
Case #3: 2.888888888889

 

考虑到平均值是一定的,所以我们就是要尽量的使得,所有的人的值都在平均值上下。

巧妙之处: 我们考虑如果多了一个人,给了这个组,那么我们看看方差会降低多少,然后取那个降得最多的

 

优先队列: 提供了一个有序的选择器,每次都可以找到最好的,并且不断的调整。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int N=2e5+10;

const double eps=1e-9;

double val[N];

double s;

struct Node{
	int id;
	int n;double x,r;
	Node(int _id=0,int _n=0,double _x=0){
		id=_id,n=_n,x=_x;
		double res1=(x-s)*(x-s)*n;
		double res2=(val[id]/(n+1)-s)*(val[id]/(n+1)-s)*(n+1);
		r=res1-res2;
	}
	bool operator<(const Node& b)const{
		return r<b.r;
	}
}p[N];



int main()
{
	int T;
	scanf("%d",&T);
	rep(kase,0,T){
		int n,m;
		scanf("%d %d",&n,&m);

		s=0;
		priority_queue<Node> q;

		rep(i,0,n){
			scanf("%lf",&val[i]);
			s+=val[i];
		}
		s/=m;

		rep(i,0,n)	q.push(Node(i,1,val[i]));

		int t=m-n;
		Node tmp;
		while(t--){
			tmp=q.top();
			q.pop();
			q.push(Node(tmp.id,tmp.n+1,val[tmp.id]/(tmp.n+1)));
		}

		int cnt=0;
		while(!q.empty()){
			p[cnt++]=q.top();
			q.pop();
		}
		double ans=0;
		rep(i,0,cnt){
		   ans+=(p[i].x-s)*(p[i].x-s)*p[i].n;
		}

	//	rep(i,0,cnt){
	//		printf("id:%d %d %.2f\n",p[i].id,p[i].n,p[i].x);
	//	}

		ans/=m;
	//	printf("s:%.2f  %.10f\n",s,ans);

		printf("Case #%d: %.10f\n",kase+1,ans);
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值