Scapegoat Gym - 101775B 思维题

版权声明:如有错误,请指出,不胜感激。 https://blog.csdn.net/qq_36424540/article/details/82913276

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;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82913276
今日推荐