CodeForces 912d fishes(优先队列+期望)
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条鱼,每个格子只能放一条,求随机撒网捞到鱼的最大期望。
题解:一开始准备用纯数学公式法做,复杂度为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(优先队列+期望)的更多相关文章
- Codeforces 912D Fishes (概率&期望,优先队列的应用)
题目链接 Fishes 题意 在一个$n*m$的矩阵中,随机选择一个$r * r$的区域覆盖. 一开始我们可以在这个$n*m$的矩阵中选择$k$个点标记为$1$. 我们要选择一个最佳的标记策略,使得 ...
- Codeforces 912D - Fishes
传送门:http://codeforces.com/contest/912/problem/D 本题是一个概率问题——求数学期望. 在一个n×m的方格中,有k个“*”.每个格子里可能有0~1个“*”. ...
- Codeforces 912D Fishs ( 贪心 && 概率期望 && 优先队列 )
题意 : 给出一个 N * M 的网格,然后给你 K 条鱼给你放置,现有规格为 r * r 的渔网,问你如果渔网随意放置去捕捞小鱼的情况下,捕到的最大期望值是多少? 分析 : 有一个很直观的想法就是 ...
- codeforces 446B(优先队列)
题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...
- codeforces 15D . Map 优先队列
题目链接 题目意思很简单nm的矩阵里, 选若干个ab的小矩阵, 定义每个矩阵的值为这个矩阵里的所有数的和-最小值*数的个数. 选小矩阵时, 优先选值最小的,然后次小的.. 知道不能选位置. 输出所有矩 ...
- CodeForces - 846F Random Query(期望)
You are given an array a consisting of n positive integers. You pick two integer numbers l and r fro ...
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- Codeforces.24D.Broken robot(期望DP 高斯消元)
题目链接 可能这儿的会更易懂一些(表示不想再多写了). 令\(f[i][j]\)表示从\((i,j)\)到达最后一行的期望步数.那么有\(f[n][j]=0\). 若\(m=1\),答案是\(2(n- ...
- codeforces 24d Broken robot 期望+高斯消元
题目传送门 题意:在n*m的网格上,有一个机器人从(x,y)出发,每次等概率的向右.向左.向下走一步或者留在原地,在最左边时不能向右走,最右边时不能像左走.问走到最后一行的期望. 思路:显然倒着算期望 ...
随机推荐
- c++ --> 你可能不知道的c++
你可能不知道的c++ 你可能不知道的 C++(一) 你可能不知道的 C++(二)
- [css 揭秘]:CSS揭秘 技巧(二):多重边框
我的github地址:https://github.com/FannieGirl/ifannie/ 源码都在这上面哦! 喜欢的给我一个星吧 多重边框 问题:我们通常希望在css代码层面以更灵活的方式来 ...
- [luogu2831][noip d2t3]愤怒的小鸟_状压dp
愤怒的小鸟 noip-d2t3 luogu-2831 题目大意:给你n个点,问最少需要多少条经过原点的抛物线将其覆盖. 注释:1<=点数<=18,1<=数据组数<=30.且规定 ...
- ASP.NET没有魔法——ASP.NET MVC Razor与View渲染
对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的界面呈现工作是由浏览器完成的,Web应用的原理是通过Http协议从服务器上获取到 ...
- 海外仓系统 COD货到付款到付功能
全球还有很多国家买家网购选择货到付款方式,例如东南亚的越南.泰国.印度尼西亚,中东的阿联酋.沙特等国家.在这些国家建立海外仓需要需要具备COD货到付款功能,麦哲伦海外仓系统已经支持COD货到到付结算相 ...
- img之间的间隙问题
前言:关于基线(base line),中线(middle line),行高(line height)的了解还是比较浅的,所以引用前辈的成果,稍带解释下 1)行高:两行文字之间"基线" ...
- 用Python登录好友QQ空间点赞
记得之前跟我女票说过,说要帮她空间点赞,点到999就不点了.刚开始还能天天记得,但是后来事情一多,就难免会忘记,前两天点赞的时候忽然觉得这样好枯燥啊,正好也在学Python,就在想能不能有什么方法能自 ...
- alpha-咸鱼冲刺day6
一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 !!!QAQ可以做到跟数据库交互了!!!!先来撒花花!(然后继续甲板) (然后就没有进展了.翻车+1s) 四,问题困难 数据库交 ...
- Beta敏捷冲刺每日报告——Day5
1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.6 00:00 -- 2017.11.7 00:00 讨论时间地点 2017.11.6 早9:30,电话会议会议 ...
- B-dya6
1.昨天的困难,今天解决的进度,以及明天要做的事情 昨天的困难:在导入导出方面遇到了困难,导出的文件不能直接导入. 今天解决的进度:完成了登录页面的背景设计,并再次测试了整个系统的功能. 明天要做的事 ...