Gym - 101775B Scapegoat (贪心)(2017ECFinal B)

B. Scapegoat
time limit per test
3.0 s
memory limit per test
256 MB
input
standard input
output
standard output

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, TT 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
Copy
3
3 6
1 2 3
5 10
5 6 7 8 9
6 6
1 1 4 5 1 4
output
Copy
Case #1: 0.000000000000
Case #2: 0.400000000000
Case #3: 2.888888888889
Note

For the first sample, Aori can let one Inkling to take the first trouble's blame, two Inklings for the second, and three Inklings for the third. The severity on each Inkling is 1 unit, so their variance should be 0.



解题思路:贪心,对于每一个替罪羊,要分配到使得 (罪恶值-平均数)^2,下降最多的个trouble上。

对于第二个样例

一开始先给每个问题分配一个替罪羊,此时这5个羊的罪恶值为

56789

计算他们的对答案的贡献,即(罪恶值-平均数)^2

然后计算增加一个羊的话,下降了多少,贪心的取最大的即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long int ll;
const int MAXN = 300095;

double a[200008];

double sum=0;
struct point{
	double tot;
	int num;
	double cur;
	double dis;
	double di;
	bool operator <(const point &b)const{
		return di<b.di;
	}
	point(double a,int b,double c){
		tot=a;
		num=b;
		cur=c;
		dis=(cur-sum)*(cur-sum)*num;
		di=dis-((tot/(num+1)-sum)*(tot/(num+1)-sum)*(num+1));	
	}
};



int main()
{
	int T;
	scanf("%d",&T);
	for(int qqq=1;qqq<=T;qqq++){
		int N,M;
		scanf("%d%d",&N,&M);

		int left=M-N;

		priority_queue<point> que;

		sum=0;
		for(int i=0;i<N;i++){
			scanf("%lf",&a[i]);
			sum+=a[i];
		}
		sum/=M;
		for(int i=0;i<N;i++){
			que.push(point(a[i],1,a[i]));
		}

		while(left--){
			point tp=que.top();
			que.pop();
			que.push(point(tp.tot,tp.num+1,tp.tot/(tp.num+1)));
		}

		double fang=0;
		for(int i=0;i<N;i++){
			int num=que.top().num;
			double f=que.top().cur;
			que.pop();
			fang+=((f-sum)*(f-sum))*num;
		}
		fang/=M;
		printf("Case #%d: %.13lf\n",qqq,fang);
	}
	

	return 0;
}







猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/80266719
今日推荐