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.
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)).
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 .
3 3 2 3
2.0000000000
12 17 9 40
32.8333333333
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条鱼,每个1*1小方格只能放一条鱼,要求随机捞鱼的期望鱼数最多,问怎么安排使得这个期望最大,输出最大期望。
解题思路:可以从最中间的格子开始,用一个优先队列维护,每次都往上下左右四个方向拓展,找出前k个最有可能被捞到的位置所对应的期望值,加起来所得到的总和即为答案
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int n, m, k, r;
map<pair<int, int>, int>mp;
struct node
{
int x, y;
LL sum;
bool operator<(const node &x)const
{
return sum < x.sum;
}
}pre,nt;
int main()
{
while (~scanf("%d%d%d%d", &n, &m, &r, &k))
{
LL ans = 0;
int cnt = 0;
mp.clear();
mp[make_pair(n / 2, m / 2)] = 1;
pre.x = n / 2, pre.y = m / 2;
pre.sum = 1LL * (min(m - 1, m / 2 + r - 1) - max(r - 1, m / 2) + 1)*(min(n - 1, n / 2 + r - 1) - max(r - 1, n / 2) + 1);
priority_queue<node>q;
q.push(pre);
while (cnt < k)
{
cnt++;
pre = q.top();
q.pop();
ans += pre.sum;
for (int i = 0; i < 4; i++)
{
int xx = pre.x + dir[i][0];
int yy = pre.y + dir[i][1];
if (mp[make_pair(xx, yy)] || xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
mp[make_pair(xx, yy)] = 1;
nt.x = xx, nt.y = yy;
nt.sum= 1LL * (min(m - 1, nt.y + r - 1) - max(r - 1, nt.y) + 1)*(min(n - 1, nt.x + r - 1) - max(r - 1, nt.x) + 1);
q.push(nt);
}
}
LL tot = 1LL * (n - r + 1)*(m - r + 1);
printf("%.9f\n", 1.0 * ans / tot);
}
return 0;
}