[CF912D]Fishes - 求数学期望,乱搞
| time limit per test | 1 second |
| memory limit per test | 256 megabytes |
| input | standard input |
| output | standard output |
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的网格,让你在里面放k条鱼,用r*r的网覆盖(共(n - r + 1)·(m - r + 1)种方法),求覆盖到鱼的数量的期望值,(保留10位小数?)
题解
放k条鱼,不如考虑每个格子放一条鱼带来的效益
写了个暴力程序,跑出每个格子的效益:
daklqw@daklqw:~/workspace/code$ ./test233 daklqw@daklqw:~/workspace/code$ ./test233
可以很方便地发现,从中间到周围,效益是不断减少的
所以我们从中间开始贪心这k条鱼,而由于这个性质,我们可以使用BFS+优先队列,每次选出一个效益最大的往四边扩展
注意到k不大,而n*m非常大,如果开二维数组会MLE,所以考虑使用set
下面献上代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
int n,m,r,k;
const int ways[][]={{,},{,},{-,},{,-}};
double ans,tot;
struct element{
int x,y;long long val;
inline bool operator<(const element & b)const{
return val<b.val;
}
};
inline long long getv(int x,int y){
int t1=max(x-r+,),t2=max(y-r+,),
t3=min(x+r-,n)-r+,t4=min(y+r-,m)-r+;//一时想不出更好的
if(t3<t1|t4<t2)return -;//防BOOM
return 1LL*(t3-t1+)*(t4-t2+);
}
priority_queue<element>q;
set<pair<int,int> >s;
int main(){
scanf("%d%d%d%d",&n,&m,&r,&k);
if(n>m)swap(n,m);//貌似没什么用
q.push((element){n+>>,m+>>,getv(n+>>,m+>>)});
s.insert(make_pair(n+>>,m+>>));
for(register int i=;i<=k;++i){//找K个
element t=q.top();q.pop();
tot+=t.val;
for(register int j=;j<;++j){//BFS
int tx=t.x+ways[j][],
ty=t.y+ways[j][];
if(tx<||ty<||tx>n||ty>m)continue;
if(s.find(make_pair(tx,ty))!=s.end())continue;
q.push((element){tx,ty,getv(tx,ty)});
s.insert(make_pair(tx,ty));
}
}
printf("%.10lf\n",tot/double(n-r+)/double(m-r+));//输出期望值
return ;
}
124ms,貌似不慢,并没有时间参加这场,所以开了模拟比赛(我们这些没rating的div2蒟蒻一起打的比赛)
[CF912D]Fishes - 求数学期望,乱搞的更多相关文章
- HDU 1099 Lottery (求数学期望)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1099 Lottery Time Limit: 2000/1000 MS (Java/Others) ...
- [BZOJ3751][NOIP2014]解方程(数学相关+乱搞)
题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...
- BZOJ2134: 单选错位(期望乱搞)
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1101 Solved: 851[Submit][Status][Discuss] Descripti ...
- uva 10828 高斯消元求数学期望
Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- 直径上的乱搞 bzoj1999求树直径上的结点+单调队列,bzoj1912负权树求直径+求直径边
直径上的乱搞一般要求出这条直径上的点集或者边集 bzoj1999:对直径上的点集进行操作 /* 给出一颗树,在树的直径上截取长度不超过s的路径 定义点u到s的距离为u到s的最短路径长度 定义s的偏心距 ...
- Luogu P1134 阶乘问题 【数学/乱搞】 By cellur925
输入输出格式 输入格式: 仅一行包含一个正整数 NN . 输出格式: 一个整数,表示最右边的非零位的值. 输入输出样例 输入样例#1: 12 输出样例#1: 6 说明 USACO Training S ...
- BZOJ-1800 飞行棋 数学+乱搞
这道题感觉就是乱搞,O(n^4)都毫无问题 1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1172 So ...
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】
黑白图像直方图 发布时间: 2017年7月9日 18:30 最后更新: 2017年7月10日 21:08 时间限制: 1000ms 内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...
随机推荐
- Markedown换行
1.换行 1⃣️行尾先加两个空格符后再按回车键,显示内容就会换行 2⃣️行尾连按两次回车键,显示内容就会换行 3⃣️举例: 第一行内容<br/>第二行内容 第一行内容<br>第 ...
- js获取url上的指定参数
function getAllUrlParams(url) { // get query string from url (optional) or window var queryString = ...
- 如何利用 CSS 的动画原理,创作一个乒乓球对打动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/rvgLzK 可交互视频教 ...
- Jquery实现对select的操作
select实现对文本框的显示和隐藏 /** * 通过select的值实现对文本框的显示和隐藏 * #id为一个select控件 * .obj为一个文本框 */ function initSelect ...
- ES6入门之Proxy
1. 概述 Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种『元编程』即对编程语言进行编程. 1.1 理解 Proxy 是在目标对象之前架设一层『拦截』,外部对对象的访问 ...
- 本地远程调试Linux 部署的web 项目
1.在linux tomcat 下面的 bin 目录下增加一个远程调试的命令: declare -x CATALINA_OPTS="-server -Xdebug -Xnoagent -Dj ...
- 多线程编程-- part5.1 互斥锁之公平锁-获取锁
基本概念 1.AQS:AbstractQueuedSynchronizer类 AQS是java中管理“锁”的抽象类,锁的许多公共方法都是在这个类中实现.AQS是独占锁(例如,ReentrantLock ...
- Hyperledger Fabric(2)共识与交易
Fabric 的网络节点本质上是互相复制的状态机,节点之间需要保持相同的账本状态.为了实现这个目的,各个节点需要通过共识( consensus )过程,对账本状态的变化达成一致性的认同. Fabric ...
- string 驻留机制
string 驻留机制 string s1 = "abc"; string s2 = "ab"; string s3 = s2 + "c" ...
- 像@Transactional一样利用注解自定义aop切片
在spring中,利用@Transactional注解可以很轻松的利用aop技术进行事物管理.在实际项目中,直接利用自定义注解实现切片可以大大的提高我们的编码效率以及代码的简洁性. 实现以上的目标,主 ...