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

Copy
3 3 2 3
output

Copy
2.0000000000
input

Copy
12 17 9 40
output

Copy
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的渔池,再给你一张r*r的网,往池子里放k条鱼,每个格子只能放一条,求随机撒网捞到鱼的最大期望。

题解:一开始准备用纯数学公式法做,复杂度为sqrt(k),但是有不少细节不好处理,索性写暴力了。

首先先转换一下思路,所有渔网网到的鱼之和等于每条鱼被几张网网到的和,然后期望就是所有鱼的期望相加。

很容易可以证明,(r,r)位置上的期望一定是最大的之一(可以尝试用反证法自己证一下)

我们把这个位置扔进优先队列,之后每次从优先队列里取出期望最大的数给答案加上,再把周围四个中没有越界没有被访问过的塞进去,一共取出k次

复杂度是(klogk)还是符合条件的。

代码如下:

#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int x,y;
double val;
friend bool operator <(const node &a, const node &b)
{
return a.val<b.val;
}
}; int n,m,r,k;
double ans=0.0;
priority_queue<node> q;
map< pair<int,int> ,int > mp; int check(int x,int y)
{
return x>=&&x<=n&&y>=&&y<=m&&(mp.count(make_pair(x,y))==);
} double get(int x,int y)
{
double xx=min(min(1.00*x,n-x+1.00),min(n-r*1.00+,1.00*r));
double yy=min(min(1.00*y,m-y+1.00),min(m-r*1.00+,1.00*r));
return xx*yy/1.00/(n-r+)/(m-r+);
} int main()
{
scanf("%d%d%d%d",&n,&m,&r,&k);
q.push({r,r,get(r,r)});
mp[make_pair(r,r)]=;
while(k--)
{
node now=q.top();
int nx=now.x,ny=now.y;
double nval=now.val;
ans+=nval;
q.pop();
if(check(nx+,ny))
{
mp[make_pair(nx+,ny)];
q.push({nx+,ny,get(nx+,ny)});
}
if(check(nx-,ny))
{
mp[make_pair(nx-,ny)];
q.push({nx-,ny,get(nx-,ny)});
}
if(check(nx,ny+))
{
mp[make_pair(nx,ny+)];
q.push({nx,ny+,get(nx,ny+)});
}
if(check(nx,ny-))
{
mp[make_pair(nx,ny-)];
q.push({nx,ny-,get(nx,ny-)});
}
}
printf("%.12lf\n",ans);
}

CodeForces 912d fishes(优先队列+期望)的更多相关文章

  1. Codeforces 912D Fishes (概率&期望,优先队列的应用)

    题目链接 Fishes 题意  在一个$n*m$的矩阵中,随机选择一个$r * r$的区域覆盖. 一开始我们可以在这个$n*m$的矩阵中选择$k$个点标记为$1$. 我们要选择一个最佳的标记策略,使得 ...

  2. Codeforces 912D - Fishes

    传送门:http://codeforces.com/contest/912/problem/D 本题是一个概率问题——求数学期望. 在一个n×m的方格中,有k个“*”.每个格子里可能有0~1个“*”. ...

  3. Codeforces 912D Fishs ( 贪心 && 概率期望 && 优先队列 )

    题意 : 给出一个 N * M 的网格,然后给你 K 条鱼给你放置,现有规格为 r * r 的渔网,问你如果渔网随意放置去捕捞小鱼的情况下,捕到的最大期望值是多少? 分析 :  有一个很直观的想法就是 ...

  4. codeforces 446B(优先队列)

    题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...

  5. codeforces 15D . Map 优先队列

    题目链接 题目意思很简单nm的矩阵里, 选若干个ab的小矩阵, 定义每个矩阵的值为这个矩阵里的所有数的和-最小值*数的个数. 选小矩阵时, 优先选值最小的,然后次小的.. 知道不能选位置. 输出所有矩 ...

  6. CodeForces - 846F Random Query(期望)

    You are given an array a consisting of n positive integers. You pick two integer numbers l and r fro ...

  7. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  8. Codeforces.24D.Broken robot(期望DP 高斯消元)

    题目链接 可能这儿的会更易懂一些(表示不想再多写了). 令\(f[i][j]\)表示从\((i,j)\)到达最后一行的期望步数.那么有\(f[n][j]=0\). 若\(m=1\),答案是\(2(n- ...

  9. codeforces 24d Broken robot 期望+高斯消元

    题目传送门 题意:在n*m的网格上,有一个机器人从(x,y)出发,每次等概率的向右.向左.向下走一步或者留在原地,在最左边时不能向右走,最右边时不能像左走.问走到最后一行的期望. 思路:显然倒着算期望 ...

随机推荐

  1. 【Python】 用户图形界面GUI wxpython IV 菜单&对话框

    更多组件 ■ 菜单栏 Menu 菜单是很多GUI必不可少的一部分.要建立菜单,必须先创建菜单栏: menuBar = MenuBar() menu = Menu() item1 = menu.Appe ...

  2. sql操作知识点个人笔记(SQLServer篇)

    实际工作中,总会遇到一些常用的或不常用的sql,这些sql可能并没多少技术含量,但对我们本身而言,一个最大的问题就是很容易忘记.对我个人而言,以前常用的,过阵子之后再用到,发现不记得了.由此得出结论, ...

  3. java中的notify和notifyAll有什么区别?

    先说两个概念:锁池和等待池 锁池:假设线程A已经拥有了某个对象(注意:不是类)的锁,而其它的线程想要调用这个对象的某个synchronized方法(或者synchronized块),由于这些线程在进入 ...

  4. struct2_拦截器知识点.

    Struts2拦截器原理: Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表,最后一个 ...

  5. centos 安装atom 笔记

    一.安装atom  "To install Atom on Linux, you can download a Debian package or RPM package either fr ...

  6. [BZOJ 2064]分裂

    2064: 分裂 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 572  Solved: 352[Submit][Status][Discuss] De ...

  7. 用Python满足满足自己的“小虚荣”

    首先声明,学习这个只是为了好玩,只是为了好玩,并不是想用这个弄虚作假,做一些不好的事情!一心想做技术人,自制自治! 我们有时候发布一篇日志,或者是一篇博文,总希望自己的浏览量能高点,这样看起来也倍有面 ...

  8. Beta冲刺NO.3

    Beta冲刺 第三天 1. 昨天的困难 1.昨天的困难主要集中在对Ajax的使用上,不熟悉这种语法,所以也就浪费了时间,导致昨天的批量删除没有完全完成. 2.由于之前的网页构造style很乱,导致修改 ...

  9. 项目Beta冲刺预热

    Beta准备 1. 讨论组长是否重选的议题和结论. 经过讨论,我们认为,经过一段时间的磨合,现任组长是不需要更换的. 2. 下一阶段需要改进完善的功能. 增加关于征信的功能,贴近选题主题 美化界面,尽 ...

  10. 201621123050 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承.抽象.多态 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 1.3 可选:使用常规方法 ...