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. [bzoj1026][SCOI2009]windy数_数位dp

    windy数 bzoj-1026 题目大意:求一段区间中的windy数个数. 注释:如果一个数任意相邻两位的差的绝对值都不小于2,这个数就是windy数,没有前导0.$区间边界<=2\cdot ...

  2. 面试:Handler 的工作原理是怎样的?

    面试场景 平时开发用到其他线程吗?都是如何处理的? 基本都用 RxJava 的线程调度切换,嗯对,就是那个 observeOn 和 subscribeOn 可以直接处理,比如网络操作,RxJava 提 ...

  3. web语义化之SEO和ARIA

    在快速理解web语义化的时候,只知道web语义化有利于SEO和便于屏幕阅读器阅读,但并不知道它是如何有利于SEO和便于阅读器阅读的,带着这个疑问,进行了一番探索总结. SEO 什么是SEO? SEO( ...

  4. bash下常用快捷键

    Ctrl-A 相当于HOME键,用于将光标定位到本行最前面Ctrl-E 相当于End键,即将光标移动到本行末尾Ctrl-B 相当于左箭头键,用于将光标向左移动一格Ctrl-F 相当于右箭头键,用于将光 ...

  5. 高级软件工程2017第6次作业--团队项目:Alpha阶段综合报告

    高级软件工程2017第6次作业--团队项目:Alpha阶段综合报告 Deadline:2017-10-30(周一)21:00pm (注:以下内容参考集大作业4,集大作业5,集大作业6,集大作业7 一. ...

  6. 20162302 实验三《敏捷开发与XP实践》实验报告

    实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:敏捷开发与XP实践 实验器材:装有IdeaU的联想拯救者80RQ 实验目的与要求:1.代码的格 ...

  7. 20145237 《Java程序设计》第三周学习总结

    20145237 <Java程序设计>第3周学习总结 教材学习内容总结 第四章主要讲了Java基本类型中的类类型,如何定义类.构造函数.使用标准类.基本类型打包器.数组复制.字符串等内容查 ...

  8. bzoj 4373 算术天才⑨与等差数列

    4373: 算术天才⑨与等差数列 Time Limit: 10 Sec  Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem.ph ...

  9. sublime安装 和 插件安装

    先从官网下载sublime   https://www.sublimetext.com/3 安装完毕后 快捷键ctrl+` 或者View->Show Console,输入如下代码(sublime ...

  10. js的构造函数共用事例

    在使用构造函数去实现一种功能时,我们有时候往往需要实现这个功能,会因此产生多个堆内对象.这样就会造成堆内存滥用.占用不该占用的空间.为此我们可以利用函数把共用的内容封装起来.放便我们的使用.很多东西其 ...