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. Django+xadmin打造在线教育平台(七)

    十.授课教师 10.1.讲师列表页 拷贝teacher-list.html和teacher-detail.html到templates目录下 先改teacher-list.html,同样继承base. ...

  2. android中activity.this跟getApplicationContext的区别

    转载: http://www.myexception.cn/android/1968332.html android中activity.this和getApplicationContext的区别 在a ...

  3. java 中的IO

    什么是文件文件可认为是相关记录或放在一起的数据集合 通过流来读写文件流是指一连串流动的字符,是以先进先出方式发送信息的通道输入输出流是相对计算机的内存来说的 字节流是八位通用字节流,字符流是16位Un ...

  4. swift 相关小随笔

    关键词 typealias 对已经存在的类重命名 let  修饰不可变值 var  修饰可变的值 lazy  懒加载修饰符,用到的时候才会加载 convenience 原方法的备用方法,方法一致,但是 ...

  5. React demo:express、react-redux、react-router、react-roter-redux、redux-thunk(一)

    近期终于把之前留下的坑填上了(说了好久的要网站重写,总算是写完了),不过最后的在线添加文章,功能虽然做了,后台没把接口加上,实在是没精力去折腾了,公司又有事要忙,现在把从0开始到完成的一个思路来写一下 ...

  6. 面试常考---html篇

    1.html5新特性,语义化 HTML5为我们提供了一系列的语义标签. 1.<section></section> 定义文档中的主体部分的节.段. 2.<article& ...

  7. C语言程序设计(基础)- 第6周作业

    一.PTA作业 完成PTA第六周作业中4个题目的思路列在博客中. 1.7-1 高速公路超速处罚 2.7-2 计算油费 3.7-3 比较大小 4.7-4 两个数的简单计算器 (必须使用switch结构实 ...

  8. 网页设计入门<一>

    俗话说:技多不压身.实习周,网页设计是之一,边学边总结... 本次网页设计基于Adobe Dreamweaver CS6开发平台,根据实习老师的暴力指导,为什么说暴力呢? 没有基础,没有预告,打开软件 ...

  9. 【Swift】iOS裁剪或者压缩后出现的白边问题

    只需要将所有的CGFloat转化为NSInteger即可 func imageScaleSize(newSize: CGSize) -> UIImage{ let width = NSInteg ...

  10. itchat 微信的使用

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