题目链接

Problem Description

Dear Liao

I never forget the moment I met with you. You carefully asked me: "I have a very difficult problem. Can you teach me?". I replied with a smile, "of course". You replied:"Given a matrix, I randomly choose a sub-matrix, what is the expectation of the number of different numbers it contains?"

Sincerely yours,

Guo

Input

The first line of input contains an integer T(T≤8) indicating the number of test cases.

Each case contains two integers, n and m (1≤n, m≤100), the number of rows and the number of columns in the grid, respectively.

The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_i,j (0≤ g_i,j < n*m).

Output

Each case outputs a number that holds 9 decimal places.

Sample Input

1

2 3

1 2 1

2 1 2

Sample Output

1.666666667

Hint

6(size = 1) + 14(size = 2) + 4(size = 3) + 4(size = 4) + 2(size = 6) = 30 / 18 = 6(size = 1) + 7(size = 2) + 2(size = 3) + 2(size = 4) + 1(size = 6)

题意:

给出一个n*m(1<=n,m<=100)的矩阵,矩阵中的每一个元素有一个颜色值ai(0<=ai<=10000),现在定义一个子矩阵的value为该子矩阵中不同颜色的数量,求所有子矩阵value的期望。

分析:

期望(也就相当于均值)=所有子矩阵的value之和 / 子矩阵个数。

首先解决子矩阵有多少个,二重循环枚举子矩形右下角的点(i,j),那么子矩阵左上角的点一定在(0,0)-(i,j)这个矩阵里,所以有i*j种。

还可以直接代公式:(n(n+1)(m+1)*m)/4

接下来我们讲重点:显然要每个颜色单独考虑,在考虑颜色i的时候,把颜色i的点看作关键点,求出 至少包含一个关键点的子矩阵个数。现在的问题是我们如何不重复不遗漏的统计个数。

先排个序(行号升序,列号升序)。把每个合法的矩阵算在序最小的那个关键点头上,这样就可以保证不重复,不遗漏。那么我们再找包含第一个关键点的矩阵的时候,显然没有任何限制,只需要包含这个点就行了。找第二个关键点的矩阵的时候,不能包含第一个点……找第i个关键点决定的矩阵的时候,不能包含1..i-1这i-1个点。

那么假设我们的图长这个样子,且灰色点是已经计数完成的点,白色点是未计数的点,黑色点是正在计数的点。

这个黑点位于(4,4)位置,我们现在要做的就是确定上下左右四个边界分别有多少种选取方式,显然白色点的序大于黑色点,黑色点的矩阵可以包含他们也可以不包含他们,因此下边界无限制,可以取到(4,5,6==n)三种方式,而由于黑点同一行的左右以及上边的行有不能包含的点,因此一个矩阵上边界对应了左右的最远边界。我们需要枚举矩阵的上边界(4,3,2,1),并且因为上边界i-1的时候上边界i要考虑的点仍然要考虑,而且要额外考虑i-1这行的灰色点。因此左右最远边界要持续进行维护。上边界是ii的时候,考虑所有ii行的灰色点,在黑点左边的去更新左边界最远点,在黑点右边的去更新有边界最远点,而刚好在黑点上边的话,说明这一行不可能作为上边界了。这个时候就结束计算黑点名下的子矩形。开始计算下一个白点。

优化:我们看第10列第1行和第3行的两个同列的灰色点,显然在上边界为3的时候,靠下的这个灰色点决定了右边界最远端,而上边界继续向上移动,右边界只可能更小,因此靠上的这个灰色点其实什么作用也没有。因此同一列只有最下边一个点有用。这样让我们在计算黑点的时候,最多只考虑之前出现的m个点,而不是理论最坏im个点,这个优化还是很关键的。因此当我们枚举(i,j)名下的矩形,上边界是xi行的时候,右边界最远是ry,左边界最远是ly,那么组合一下就得到了(n-i+1)(j-ly+1)(ry-j+1)个贡献(下边界方案左边界方案右边界方案,上边界已经确定是xi)。上边讲的一个跳出条件仍然有效。

整个算法复杂度严格小于 mn(n/2+m)(点数平摊上边界枚举数量&最多需要考虑的点),实际上跑起来是快的飞起,因为中间会跳出,而且要考虑的点没那么多。

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX = 105;
int m,n;
int mp[MAX][MAX];
int bottom[MAX];
vector< pair <int,int> > Color[MAX*MAX];///用于存储每个颜色对应的坐标点
vector<int> yIndex[MAX];///每个颜色已经遍历过的点的行号
long long calc(int col)
{
memset(bottom,0,sizeof(bottom));
memset(yIndex,0,sizeof(yIndex));
long long ans = 0;
///遍历所有的这个颜色的点
for (vector<pair <int,int> > :: iterator it=Color[col].begin(); it!=Color[col].end(); it++)
{
int ni = it->first,nj = it->second;///获取横、纵坐标
for (int i = 1; i<=m; i++)///在每一列里面找,看有没有这个颜色的点
{
if (bottom[i])
yIndex[bottom[i]].push_back(i);///把对应的列放入到行号数组里
}
int yl=1,yr=m;
bool br = false;
for (int ii = ni; ii>=1; ii--)
{
///在每一行里面找有没有这个颜色的点
for (vector<int>::iterator it = yIndex[ii].begin(); it!=yIndex[ii].end(); it++)
{
int yy = *it;
if (yy<nj)
yl = max(yl,yy+1);///寻找左边界
else if (yy>nj)
yr = min (yr,yy-1);///右边界
else
{
br = true;
break;
}
}
if (br) break;
ans+=(n-ni+1)*(nj-yl+1)*(yr-nj+1);
}
bottom[nj] = ni;
}
return ans;
}
void solve()
{
long long ans = 0;
for (int i = 0; i<=n*m; i++)
{
if (!Color[i].empty())
{
ans +=calc(i);
}
}
double anss = ((double)(4*ans))/(n*(n+1)*m*(m+1));
printf("%.9lf\n",anss);
}
int main()
{
int Cas;
scanf("%d",&Cas);
while (Cas--)
{
memset(Color,0,sizeof(Color));
scanf("%d%d",&n,&m);
for (int i=1; i<=n; i++)
{
for (int j = 1; j<=m; j++)
{
scanf("%d",&mp[i][j]);
Color[mp[i][j]].push_back(make_pair(i,j));
}
}
for (int i=0; i<=n*m; i++)
{
if (!Color[i].empty())
{
sort(Color[i].begin(),Color[i].end());
}
}
solve();
}
return 0;
}

2017ACM暑期多校联合训练 - Team 2 1008 HDU 6052 To my boyfriend (数学 模拟)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  2. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  3. 2017ACM暑期多校联合训练 - Team 7 1008 HDU 6127 Hard challenge (极角排序)

    题目链接 Problem Description There are n points on the plane, and the ith points has a value vali, and i ...

  4. 2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)

    题目链接 Problem Description We define the distance of two strings A and B with same length n is disA,B= ...

  5. 2017ACM暑期多校联合训练 - Team 5 1001 HDU 6085 Rikka with Candies (模拟)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  6. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  7. 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)

    题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...

  8. 2017ACM暑期多校联合训练 - Team 2 1001 HDU 6045 Is Derek lying? (模拟)

    题目链接 Problem Description Derek and Alfia are good friends.Derek is Chinese,and Alfia is Austrian.Thi ...

  9. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

随机推荐

  1. python获取toast 验证

    appium版本 1.6.3  desired_caps['automationName']='uiautomator2'    def _find_toast(self,message,timeou ...

  2. 第114天:Ajax跨域请求解决方法(二)

    一.什么是跨域 我们先回顾一下域名地址的组成: http:// www . google : 8080 / script/jquery.js   http:// (协议号)       www  (子 ...

  3. 【ASP.NET Core 】ASP.NET Core 源码学习之 Logging[1]:Introduction

    在ASP.NET 4.X中,我们通常使用 log4net, NLog 等来记录日志,但是当我们引用的一些第三方类库使用不同的日志框架时,就比较混乱了.而在 ASP.Net Core 中内置了日志系统, ...

  4. caffe框架下目标检测——faster-rcnn实战篇操作

    原有模型 1.下载fasrer-rcnn源代码并安装 git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git 1) ...

  5. 高斯消元模板(pascal)

    洛谷P3389评测 program rrr(input,output); const eps=1e-8; var a:..,..]of double; n,i,j,k:longint; t:doubl ...

  6. 【JavaScript】20款漂亮的css字体

    样式一: body { margin: 0; padding: 0; line-height: 1.5em; font-family: "Times New Roman", Tim ...

  7. 洛谷 P2647 最大收益

    我是题面 恩,贪心,鉴定完毕. 一个物品是否放进来,取决于它是否能对答案做出贡献. 那物品i的贡献就是\(w[i]-r[i]\) 可是收益的减少是会叠加的 那就是\(w[i]-j*r[i]\),j表示 ...

  8. Doves and bombs UVA - 10765(统计割顶所连接的连通块的数量)

    题意:给定一个n个点的连通的无向图,一个点的“鸽子值”定义为将它从图中删去后连通块的个数. 求对应的点 和 每个点的“鸽子值” 用一个数组在判断割顶的那个地方 累加标记一下所连接的连通块的数量即可 初 ...

  9. 【刷题】BZOJ 5293 [Bjoi2018]求和

    Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k 次方和,而且每次的k 可能是不同的.此处节点深度的定义是这个节点到 ...

  10. window上安装elasticserach

    提供一个百度云链接下载elasticsearch (链接:https://pan.baidu.com/s/1sk8PYjV 密码:l586) 测试达到目的:安装elasticsearch后再安装hea ...