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. 《HelloGitHub》第 24 期(两周年)

    公告 今天是<HelloGitHub>月刊 两周年.当时发布第一期的时候,根本没有想到可以走到现在. 这两年,HelloGitHub 项目有过辉煌的时刻:连续 3 天 GitHub 趋势首 ...

  2. JavaScript(第十四天)【面向对象和原型】

    学习要点: 1.学习条件 2.创建对象 3.原型 4.继承 ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标志,那就是类的概念,而通过类可以创建 ...

  3. 团队作业6——展示博客(Alpha版本)

    Deadline: 2017-12-3  23:00PM,以博客发表日期为准   评分基准 按时交 - 有分,检查的项目包括后文的两个方面 团队成员介绍 Alpha阶段进展 团队合作,各成员分工 Be ...

  4. Week02-Java基本语法与类库

    1. 本周学习总结 本周主要学习了包装类,明白了包装类比基本数据类型的优点(见书面作业4.2),了解了自动装箱,自动拆箱的概念和区别(见书面作业4.1),知道了java中的引用类似C语言中的指针,明白 ...

  5. itchat 微信的使用

    #coding=utf8 import itchat # 自动回复 # 封装好的装饰器,当接收到的消息是Text,即文字消息 @itchat.msg_register('Text') def text ...

  6. SWFUpload文件上传详解

    SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...

  7. Java看书学习笔记

    1.POM:maven ,项目管理工具存放Jar包的文件2.mybatis-generator-core-1.3.2 生成文件 生成语句: java -jar mybatis-generator-co ...

  8. SQL Server元数据损坏(metadata corruption)修复

    在升级一个SQL Server 2000的数据库时,遇到了一致性错误,其中有几个错误是元数据损坏(metadata corruption),特意研究了一下这个案例,因为以前也零零散散的遇到过一些一致性 ...

  9. Apache虚拟主机实战

    [root@localhost ~]# wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.32.tar.bz2  [root@localho ...

  10. Python3 re模块(正则表达式)

    一:什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. (在Python中)它内嵌在Python中,并通过r ...