[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 描述 在一个矩形的灰度图像上,每个 ...
随机推荐
- python-day37(正式学习)
前景回顾 抢票系统的代码优化,使用了Lock类 from multiprocessing import Process,Lock import os,time,json with open('user ...
- jinja2介绍
jinja2介绍 jinja2是Flask作者开发的一个模板系统,起初是仿django模板的一个模板引擎,为Flask提供模板支持,由于其灵活,快速和安全等优点被广泛使用. jinja2的优点 jin ...
- QThread::wait(),一直以来我以为它阻塞的是QThread对象,可是我现在明白,原来阻塞的是这个对象所在的线程(通常是主线程)——所有事情源于 QThread 的事件循环——如果使用继承QThread这一方法,QThread::quit()没有效果,因为这个线程根本就不需要事件循环
近日,使用QThread,一些问题百思不得其解,看过大牛的文章,恍然大悟啊. 原文 http://hi.baidu.com/dbzhang800/item/c14c97dd15318d17e1f46f ...
- 基于CentOS系统部署EPICS环境
1.虚拟机安装CentOS系统2.打开终端,以root账户登录3.进入/usr/local目录下,新建文件夹epics,并进入该文件夹4.在/usr/local/epics目录下,执行wget htt ...
- luogu P3226 [HNOI2012]集合选数
luogu 因为限制关系只和2和3有关,如果把数中2的因子和3的因子都除掉,那剩下的数不同的数是不会相互影响,所以每次考虑剩下的数一样的一类数,答案为每类数答案的乘积 如果选了一个数,那么2的因子多1 ...
- axios与ajax的区别及优缺点
区别:axios是通过Promise实现对ajax技术的一种封装,就像jquery对ajax的封装一样,简单来说就是ajax技术实现了局部数据的刷新,axios实现了对ajax的封装,axios有的a ...
- python 运算符与流程控制
运算符与流程控制 运算符 赋值运算 用'='表示,'='的左边只能是变量 算术运算 +.-.*:加.减.乘 /:除法运算,运算结果为浮点数 //:除法运算,运算结果为整数(商) %:求余 **:求幂 ...
- (转)FPS游戏服务器设计的问题
FPS游戏服务器设计的问题出处:http://www.byteedu.com/thread-20-1-1.html一.追溯 去gameloft笔试,有一个题目是说: 叫你去设计一个FPS(第一人称射击 ...
- (转)Java8内存模型-永久代(PermGen)和元空间(Metaspace)
原文链接:https://www.cnblogs.com/paddix/p/5309550.html 一.JVM内存模型 根据jvm规范,jvm内存共分为虚拟机栈.堆.方法区.程序计算器.本地方法栈五 ...
- 11、Nginx反向代理服务
1Nginx代理服务基本概述 1.代理一词并不陌生, 该服务我们常常用到如(代理理财.代理租房.代理收货等等),如下图所示 2.在没有代理模式的情况下,客户端和Nginx服务端,都是客户端直接请求服务 ...