2016 Multi-University Training Contest 1 T3
题目要求出所有合法点对间的最短路径的平均值,因此我们应当求出所有合法最短点对的最 短路径之和,再除以合法点对个数。
题目中Guard之间有着很不自然的制约关系,每个Guard的周围和同行、列都不能有其余的 Guard,但不可路过的格子却只有本身一格。由此发现,任意两点间的最短路径只会至多被 一个Guard所干扰。
我们可以先算出没有Guard制约的所有最短路径长度之和,再分别对于每个 Guard求出必须经过他的点对个数,原本必须经过它的最短路要多绕两格。
对于第i行每个#点,他对于上面第j行的每个#点在纵坐标路径长度的贡献是abs(i-j),列也一样。然后就可以o(n*m)的算出路径和。
对于G点,和他在同一列的和不同列的可以分开算。
#include<cstring>
#include<cstdio>
#define mem(a) memset(a,0,sizeof(a))
int gl[],gh[],_,i,j,n,m;
double x,sum,ans,numl[],numh[],p;
char c[][];
int main()
{
//freopen("1.in","r",stdin); freopen("1.out","w",stdout);
scanf("%d",&_);
while (_--)
{
sum=;
mem(numh);mem(numl);mem(gl);mem(gh);
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
{
scanf("%s",c[i]+);
for (j=;j<=m;j++)
{
if (c[i][j]=='#')
{
sum+=;
numl[j]+=;
numh[i]+=;
}
else
{
gl[j]=i;
gh[i]=j;
}
}
}
ans=;sum=sum*sum;
for (i=;i<=n;i++)
{
for (j=;j<i;j++)
{
x=(i-j)*numh[j]/sum;
ans+=x*numh[i];
}
for (j=i+;j<=m;j++)
{
x=(j-i)*numh[j]/sum;
ans+=x*numh[i];
}
}
for (i=;i<=m;i++)
{
for (j=;j<i;j++)
{
x=(i-j)*numl[j]/sum;
ans+=x*numl[i];
}
for (j=i+;j<=m;j++)
{
x=(j-i)*numl[j]/sum;
ans+=x*numl[i];
}
}
for (i=;i<=n;i++)
for (j=;j<=m;j++)
if(c[i][j]=='G')
ans+=((i-)*(n-i)*+(j-)*(m-j)*)/sum; for (j=;j<=n;j++){
if (gh[j-]== || gh[j]==) continue;
if (gh[j-]<gh[j]){
p=(gh[j-]-)*(m-gh[j])/sum;
for (i=j-;i>;i--){
if (gh[i]== || gh[i]>gh[i+]) break;
p+=1.0*(gh[i]-)*(m-gh[j])/sum;
}
}else {
p=(m-gh[j-])*(gh[j]-)/sum;
for (i=j-;i>;i--){
if (gh[i]== || gh[i]<gh[i+]) break;
p+=(m-gh[i])*(gh[j]-)/sum;
}
}
ans+=4.0*p;
}
for (j=;j<=m;j++){
if (gl[j-]== || gl[j]==) continue;
if (gl[j-]<gl[j]){
p=(gl[j-]-)*(n-gl[j])/sum;
for (i=j-;i>;i--){
if (gl[i]== || gl[i]>gl[i+]) break;
p+=(gl[i]-)*(n-gl[j])/sum;
}
}else {
p=(n-gl[j-])*(gl[j]-)/sum;
for (i=j-;i>;i--){
if (gl[i]== || gl[i]<gl[i+]) break;
p+=(n-gl[i])*(gl[j]-)/sum;
}
}
ans+=4.0*p;
} printf("%.4f\n",ans);
}
return ;
}
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
17652872 | 2016-07-20 19:02:27 | Accepted | 5725 | 717MS | 2528K | 2012 B | G++ | lbz007 |
2016 Multi-University Training Contest 1 T3的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
- 2016 Al-Baath University Training Camp Contest-1 G
Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...
- 2016 Al-Baath University Training Camp Contest-1 D
Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...
随机推荐
- Tozan and Gezan(x*y<a*b)
E - Tozan and Gezan Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Yo ...
- __filename意思
__filename 表示当前正在执行的脚本的文件名.它将输出文件所在位置的绝对路径,且和命令行参数所指定的文件名不一定相同. 如果在模块中,返回的值是模块文件的路径.
- Cisco2960 交换机密码破解方法
1. 启用超级终端并出现连接界面2. 拔掉交换机的电源3. 按下交换机上的 Mode按钮, 与此同时重新插上交换机的电源线4. 当交换机左边的sys 灯会慢慢的闪动, ( 细心的话会发现部分灯红色的闪 ...
- Android API Levels
Android API Levels(转) 在本文中 API的级别是什么? 在Android中使用API级别 开发者需要考虑的内容 应用程序的向前兼容性 应用程序的向后兼容性 平台版本和API级别的选 ...
- Spring源码阅读笔记03:xml配置读取
前面的文章介绍了IOC的概念,Spring提供的bean容器即是对这一思想的具体实现,在接下来的几篇文章会侧重于探究这一bean容器是如何实现的.在此之前,先用一段话概括一下bean容器的基本工作原理 ...
- Java版飞机订票系统
关注微信公众号:Worldhello 回复 飞机订票系统 可获得系统源代码并可加群讨论交流 数据结构课程设计题目: [飞机订票系统] 通过此系统可以实现如下功能 ...
- 安装Kubernetes集群时遇到的问题及解决方法
在搭建Kubernetes集群时遇到一些问题,记录在这里. 搭建过程在另一篇文章:VirtualBox上使用kubeadm安装Kubernetes集群 1. 虚拟机安装完CentOS7登录时遇到war ...
- LeetCode--二叉树1--树的遍历
LeetCode--二叉树1--树的遍历 一 深度遍历 深度遍历里面由 三种遍历方式,两种实现方法.都要熟练掌握. 值得注意的是,当你删除树中的节点时,删除过程将按照后序遍历的顺序进行. 也就是说,当 ...
- koa01
1.koa简介 koa是express团队开发的一个更加轻量级的服务端开发框架,也是未来的趋势 2.安装 npm i -g koa-generator //全局安装koa脚手架 3.创建项目 koa2 ...
- 神奇的background——绘制图形
相信大家在平时工作中少不了会被要求在某些元添加一些特殊的背景图片,这时候通常就拿起ps就是切切切.不说这种方式麻烦,有ui给你切好的情况已经不错,没有的就有自己动手.还可能有需要切一整张超大图的情况. ...