题目链接:http://codeforces.com/gym/101775/problem/B

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 × 10^5.
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

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.

题意:

现在某人闯祸了,产生了 $N$ 个锅,每个锅有个严重点数,现在可以安排 $M$ 个替罪羊去背锅。

每个替罪羊最多只能背一个锅。若一只羊背一个锅,则该锅的严重点数全部算在它头上;若多只羊背同一个锅,则每个羊分到该锅的一部分的严重点数。

现在考虑一种安排方案,使得所有的身上的严重点数的方差最小。

题解:

先考虑上 $N$ 只羊一一对应地背 $N$ 个锅,剩下 $M-N$ 个替罪羊身上严重点数均为 $0$,当然这样并不是最优解。

应当把再剩下 $M-N$ 个替罪羊安排进 $N$ 个锅里分摊责任,使得方差减小。考虑贪心的思路,每次安排进去一只羊,都要使得方差减小最多。

考虑将新来的羊安排到某个任务,该任务严重点数为 $a[i]$,且原来的背锅羊数是 $num$,那么首先每个替罪羊分到的严重点数的平均数肯定是不变的 $\overline{X} = \frac{a[1]+ a[2]+ \cdots + a[n]}{m}$,因此它原本对方差的贡献为 $\frac{1}{m} \cdot num \cdot (\frac{a[i]}{num}-\overline{X})^2$。

而现在新加进去一个替罪羊,这个任务对方差的新的贡献为 $\frac{1}{m} \cdot (num+1) \cdot (\frac{a[i]}{num+1}-\overline{X})^2$。

显然,关键的差值就是 $\Delta = num \cdot (\frac{a[i]}{num}-\overline{X})^2 -(num+1) \cdot (\frac{a[i]}{num+1}-\overline{X})^2 = \frac{a[i]^2}{num \cdot (num+1)} - \overline{X}^2$,因此我们让每次挑一个任务让它的 $\Delta + \overline{X}^2 = \frac{a[i]^2}{num \cdot (num+1)}$ 最大就好了,这个可以用优先队列。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+;
const int eps=1e-;
int n,m;
int a[maxn];
struct Node{
int id;
int num;
Node(int _id,int _num) {
id=_id, num=_num;
}
inline double calc()const {
return a[id]*a[id]*1.0/num/(num+);
}
bool operator<(const Node& oth)const {
return this->calc() < oth.calc()+eps;
}
};
priority_queue<Node> Q;
int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%d%d",&n,&m);
double aver=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
aver+=a[i];
Q.push(Node(i,));
}
aver/=m;
for(int rest=m-n;rest>;rest--)
{
Node now=Q.top(); Q.pop();
Q.push(Node(now.id,now.num+));
}
double ans=;
while(!Q.empty())
{
Node now=Q.top(); Q.pop();
ans+=now.num*pow(a[now.id]*1.0/now.num-aver,2.0);
}
ans/=m;
printf("Case #%d: %.9lf\n",kase,ans);
}
}

Gym 101775B - Scapegoat - [贪心+优先队列]的更多相关文章

  1. 【贪心】【堆】Gym - 101775B - Scapegoat

    题意:有n个事件,每个事件有一个严重程度,m个人(m>=n),你要让m个人去背锅,每个人只能背一个事件的锅,但是一个事件可以由很多人背.让你使得这m个人所承受的严重程度的方差最小化. 考虑一开始 ...

  2. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  3. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  4. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  6. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  7. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

  8. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  9. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

随机推荐

  1. GopherChina 2018

    https://github.com/gopherchina/conference/tree/master/2018

  2. exception The valid characters are defined in RFC 7230 and RFC 3986

      1.情景展示 当你使用浏览器进行问号传参与后台进行交互时,会报这个异常. tomcat控制台报错信息如下: The valid characters are defined in RFC 7230 ...

  3. go微服务框架go-micro深度学习-目录

    go微服务框架go-micro深度学习(一) 整体架构介绍 go微服务框架go-micro深度学习(二) 入门例子 go微服务框架go-micro深度学习(三) Registry服务的注册和发现 go ...

  4. linux清除缓存

    linux清除缓存:需要root权限$ sync$ echo 3 >/proc/sys/vm/drop_caches 上面的echo 3 是清理所有缓存 echo 0 是不释放缓存 echo 1 ...

  5. 转载:MVC升级以后出现"当前上下文中不存在ViewBag"的问题解决

    MVC升级以后出现"当前上下文中不存在ViewBag"的问题解决 把自己的项目从MVC4升级到了MVC5,结果问题一大堆,View的设计环境出现了"当前上下文中不存在Vi ...

  6. DES算法原理完整版

    1.所需参数 key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 2.初始置换 DES算法使用64位的密钥key将64位的 ...

  7. 使用phpstorm进行PHP断点调试

    PHP开发中都说一个会偷懒的程序员才是合格的程序员,在PHP开发中调试是必须要有的,可能要重复很多次的去调试,一次又一次,今天我们就来教教大家如何偷懒的,那么就来讲讲使用phpstorm进行偷懒吧! ...

  8. 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx EF 6 Code-F ...

  9. python 验证码识别示例(一) 某个网站验证码识别

    某个招聘网站的验证码识别,过程如下 一: 原始验证码: 二: 首先对验证码进行分析,该验证码的数字颜色有变化,这个就是识别这个验证码遇到的比较难的问题,解决方法是使用PIL 中的  getpixel  ...

  10. macOS 下 PHPStorm + Xdebug 调试 Docker 环境中的代码

    0x00 描述 宿主机是 mac mini,构建的项目在 docker 中,所以需要在 PHPStorm 上配置 Xdebug 进行远程代码调试. 0x01 环境 宿主机:macOS High Sie ...