time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of size n × m, divided into cells of size 1 × 1, inhabited by tiny evil fishes (no more than one fish per cell, otherwise they'll strife!).

The gift bundle also includes a square scoop of size r × r, designed for fishing. If the lower-left corner of the scoop-net is located at cell (x, y), all fishes inside the square (x, y)...(x + r - 1, y + r - 1) get caught. Note that the scoop-net should lie completely inside the pond when used.

Unfortunately, Sasha is not that skilled in fishing and hence throws the scoop randomly. In order to not frustrate Sasha, Misha decided to release k fishes into the empty pond in such a way that the expected value of the number of caught fishes is as high as possible. Help Misha! In other words, put k fishes in the pond into distinct cells in such a way that when the scoop-net is placed into a random position among (n - r + 1)·(m - r + 1) possible positions, the average number of caught fishes is as high as possible.

Input

The only line contains four integers n, m, r, k (1 ≤ n, m ≤ 105, 1 ≤ r ≤ min(n, m), 1 ≤ k ≤ min(n·m, 105)).

Output

Print a single number — the maximum possible expected number of caught fishes.

You answer is considered correct, is its absolute or relative error does not exceed 10 - 9. Namely, let your answer be a, and the jury's answer be b. Your answer is considered correct, if .

Examples
input
3 3 2 3
output
2.0000000000
input
12 17 9 40
output
32.8333333333
Note

In the first example you can put the fishes in cells (2, 1), (2, 2), (2, 3). In this case, for any of four possible positions of the scoop-net (highlighted with light green), the number of fishes inside is equal to two, and so is the expected value.

题目大意

给一个n*m的网格,让你在里面放k条鱼,用r*r的网覆盖(共(n - r + 1)·(m - r + 1)种方法),求覆盖到鱼的数量的期望值,(保留10位小数?)


题解

放k条鱼,不如考虑每个格子放一条鱼带来的效益

写了个暴力程序,跑出每个格子的效益:

 daklqw@daklqw:~/workspace/code$ ./test233

 daklqw@daklqw:~/workspace/code$ ./test233

可以很方便地发现,从中间到周围,效益是不断减少的

所以我们从中间开始贪心这k条鱼,而由于这个性质,我们可以使用BFS+优先队列,每次选出一个效益最大的往四边扩展

注意到k不大,而n*m非常大,如果开二维数组会MLE,所以考虑使用set

下面献上代码:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
int n,m,r,k;
const int ways[][]={{,},{,},{-,},{,-}};
double ans,tot;
struct element{
int x,y;long long val;
inline bool operator<(const element & b)const{
return val<b.val;
}
};
inline long long getv(int x,int y){
int t1=max(x-r+,),t2=max(y-r+,),
t3=min(x+r-,n)-r+,t4=min(y+r-,m)-r+;//一时想不出更好的
if(t3<t1|t4<t2)return -;//防BOOM
return 1LL*(t3-t1+)*(t4-t2+);
}
priority_queue<element>q;
set<pair<int,int> >s;
int main(){
scanf("%d%d%d%d",&n,&m,&r,&k);
if(n>m)swap(n,m);//貌似没什么用
q.push((element){n+>>,m+>>,getv(n+>>,m+>>)});
s.insert(make_pair(n+>>,m+>>));
for(register int i=;i<=k;++i){//找K个
element t=q.top();q.pop();
tot+=t.val;
for(register int j=;j<;++j){//BFS
int tx=t.x+ways[j][],
ty=t.y+ways[j][];
if(tx<||ty<||tx>n||ty>m)continue;
if(s.find(make_pair(tx,ty))!=s.end())continue;
q.push((element){tx,ty,getv(tx,ty)});
s.insert(make_pair(tx,ty));
}
}
printf("%.10lf\n",tot/double(n-r+)/double(m-r+));//输出期望值
return ;
}

124ms,貌似不慢,并没有时间参加这场,所以开了模拟比赛(我们这些没rating的div2蒟蒻一起打的比赛)

[CF912D]Fishes - 求数学期望,乱搞的更多相关文章

  1. HDU 1099 Lottery (求数学期望)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1099 Lottery Time Limit: 2000/1000 MS (Java/Others)   ...

  2. [BZOJ3751][NOIP2014]解方程(数学相关+乱搞)

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...

  3. BZOJ2134: 单选错位(期望乱搞)

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1101  Solved: 851[Submit][Status][Discuss] Descripti ...

  4. uva 10828 高斯消元求数学期望

    Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name ...

  5. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  6. 直径上的乱搞 bzoj1999求树直径上的结点+单调队列,bzoj1912负权树求直径+求直径边

    直径上的乱搞一般要求出这条直径上的点集或者边集 bzoj1999:对直径上的点集进行操作 /* 给出一颗树,在树的直径上截取长度不超过s的路径 定义点u到s的距离为u到s的最短路径长度 定义s的偏心距 ...

  7. Luogu P1134 阶乘问题 【数学/乱搞】 By cellur925

    输入输出格式 输入格式: 仅一行包含一个正整数 NN . 输出格式: 一个整数,表示最右边的非零位的值. 输入输出样例 输入样例#1: 12 输出样例#1: 6 说明 USACO Training S ...

  8. BZOJ-1800 飞行棋 数学+乱搞

    这道题感觉就是乱搞,O(n^4)都毫无问题 1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1172 So ...

  9. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

随机推荐

  1. spring依赖注入三种方式

    一.构造器注入 构造器注入是在程序中实现构造器,可以注入任意类型,如自定义类,集合,String等,注:构造器所有有final修饰的变量都必须在构造方法中注入. 二.设值注入(setter方式注入) ...

  2. Maven简单项目及命令使用

    项目结构如下 HelloWorld.java package pak1; public class HelloWorld { public String sayHello(){ return &quo ...

  3. CodeForce20C

    这是一个裸的最短路的模板题,但是它需要输出路径. 用dijkstra的话首先敲一个最短路的板子,其次开一个数组p[]来记录路径,但是怎么存呢?我们需要记录每一个点的前驱,因为如果记录后边的话,一个点可 ...

  4. CSP 画图(201512-3)

    问题描述 用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术.例如,下图是用 ASCII 字符画出来的 CSPRO 字样. ..____.____..____. ...

  5. GitHub从小白到熟悉<三>

    上传文件

  6. 从入门到自闭之Python内置函数

    内置函数一 eval:执行字符串类型的代码 exac:执行字符串社类型的代码 eval与exac 禁止使用 hash()作用就是区分可变数据类型与不可变数据类型 # print(hash(" ...

  7. Chromium浏览器启动参数

    序号 参数 说明1 --allow-outdated-plugins 不停用过期的插件.2 --allow-running-insecure-content 默认情况下,https 页面不允许从 ht ...

  8. sql server join联结

    join学习起来有点乱,现做如下整理: table A id abc 1 a 2 b 3 c 4 d table B id abc 1 e 2 a 3 f 4 c --join或者inner join ...

  9. ffmpeg3.3.2命令行参数笔记

    组成: 1.libavformat:用于各种音视频封装格式的生成和解析,包括获取解码所需信息以生成解码上下文结构和读取音视频帧等功能,包含demuxers和muxer库: 2.libavcodec:用 ...

  10. 17、RAID和LVM

    一.RAID 1.什么是raid 磁盘阵列(Redundant Arrays of Independent Drives,RAID),有"独立磁盘构成的具有冗余能力的阵列"之意. ...