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 ...
随机推荐
- JDBC源码分析(加载过程)
public static void main(String[] args) { String url = "jdbc:mysql://172.16.27.11:3306/jdbcT ...
- 转-Zeus资源调度系统介绍
摘要: 本文主要概述阿里巴巴Zeus资源调度系统的背景和实现思路. 本文主线:问题.解决方案.依赖基础知识.工程实践.目标.经验分享.立足企业真实问题.常规解决策略,引出依赖的容器技术.实践方案,所有 ...
- Allure介绍
以下内容基于pytest的框架进行展示: 什么是Allure Allure是一个独立的报告插件,生成美观易读的报告,目前支持语言:Java, PHP, Ruby, Python, Scala, C#. ...
- 正负小数js正则表达式
var reg = /^(([1-9]\d+(.[0-9]{1,4})?|\d(.[0-9]{1,4})?)|([-]([1-9]\d+(.[0-9]{1,4})?|\d(.[0-9]{1,4})?) ...
- 吴裕雄--天生自然 R语言开发学习:基本图形(续二)
#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...
- MySQL树形结构的数据库表设计和查询
1.邻接表(Adjacency List) 实例:现在有一个要存储一下公司的人员结构,大致层次结构如下: 那么怎么存储这个结构?并且要获取以下信息: 1.查询小天的直接上司. 2.查询老宋管理下的直属 ...
- 第12章 Reference-RIL运行框架
Reference-RIL完成两部分处理逻辑: 与LibRIL交互完成RIL消息的处理. 与Modem通信模块交互完成AT命令的执行. Reference-RIL的运行机制 主要涉及以下几个方面: R ...
- 张益肇:AI 医疗,微软有哪些布局?
编者按:近几年来,医疗和人工智能碰撞出了相当多的火花,大量资金短期投入到医疗领域当中.然而在微软亚洲研究院副院长张益肇博士看来,人工智能医疗是一场持久战,大家一定要沉下心多调研.多思考.多学习. 人工 ...
- Java编程Tips
原文: Java编程中"为了性能"尽量要做到的一些地方 作者: javatgo 最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于 ...
- iPhone6爆炸真是小概率事件吗?
前不久,央视新闻报道,根据上海市消费者权益保护委员会统计,2016年9月到11月,共接到8名消费者投诉,反映其苹果手机在正常使用或者正常充电的情况下突然爆炸.此外,苹果手机还被投诉存在自动关机等问题, ...