To my boyfriend

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1246    Accepted Submission(s): 556

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)

Source
【题意】给你一个矩阵,求子矩阵中颜色种类数的期望。
【分析】考虑算每一个位置的贡献,然后现在就要定一个提供贡献的规则,假设提供贡献的该种颜色是该子矩阵中该种颜色分布的左上  方,那么只需算出包括这个点的有效子矩阵个数即可。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e2+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,m;
int a[N][N];
int l[N],r[N],pos[N][N*N];
ll ans;
void solve(int x,int y,int col){
int ly=,ry=m+;
for(int i=;i<=n;i++)l[i]=,r[i]=m+;
for(int i=;i<y;i++)l[pos[i][col]]=i;
for(int i=m;i>y;i--)r[pos[i][col]]=i;
for(int i=x;i>pos[y][col];i--){
ly=max(ly,l[i]);
ry=min(ry,r[i]);
ans+=(ll)(n-x+)*(ry-y)*(y-ly);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
met(pos,);ans=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&a[i][j]);
solve(i,j,a[i][j]);
pos[j][a[i][j]]=i;
}
}
ll all=(ll)n*(n+)*m*(m+)/;
printf("%.9f\n",(double)ans/all);
}
return ;
}

HDU 6052 To my boyfriend(概率 贡献)的更多相关文章

  1. HDU 6052 - To my boyfriend | 2017 Multi-University Training Contest 2

    说实话不是很懂按题解怎么写,思路来源于 http://blog.csdn.net/calabash_boy/article/details/76272704?yyue=a21bo.50862.2018 ...

  2. HDU 6052 To my boyfriend(悬线法)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6052 [题目大意] 给出一个数字矩阵,求子矩阵期望数字种数 [题解] 我们统计[x,y]为其所表示 ...

  3. hdu 6052 To my boyfriend

    题目 OvO click here http://acm.hdu.edu.cn/showproblem.php?pid=6052 (2017 Multi-University Training Con ...

  4. 2017ACM暑期多校联合训练 - Team 2 1008 HDU 6052 To my boyfriend (数学 模拟)

    题目链接 Problem Description Dear Liao I never forget the moment I met with you. You carefully asked me: ...

  5. HDU 6052 To my boyfriend(容斥+单调栈)

    题意:对于一个n*m的方格,每个格子中都包含一种颜色,求出任意一个矩形包含不同颜色的期望. 思路: 啊啊啊啊啊,补了两天,总算A了这道题了,简直石乐志,前面的容斥还比较好写,后面的那个>13那个 ...

  6. HDU 3853:LOOPS(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3853 LOOPS Problem Description   Akemi Homura is a M ...

  7. HDU 3076 ssworld VS DDD 概率dp,无穷级数,oj错误题目 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3076 不可思议的题目,总之血量越少胜率越高,所以读取时把两人的血量交换一下 明显每一轮的胜率和负率都是固定的,所 ...

  8. HDU 5781 ATM Mechine (概率DP)

    ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...

  9. hdu 4762 Cut the Cake概率公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4762 题目大意:一个圆形蛋糕,现在要分成M个相同的扇形,有n个草莓,求n个草莓都在同一个扇形上的概率. ...

随机推荐

  1. tap事件的原理详解

    点击事件延迟问题所在: 在移动端中,由于两次触摸是放大操作,,所以当你点击一次的时候,浏览器会等待300ms,看用户是否会进行第二次点击,如果没有的话,才会执行点击事件 为什么要解决: 随着h5游戏, ...

  2. 使用RVM轻松部署Ruby环境

    Ruby用得不多,但发现有业务需要部署指定的版本和插件.起初找了一些Fedora的src.rpm重新打包,发现依赖问题比较多,最终还是费劲的把el6的包编出来了. 不巧今天又有业务要求el5的包,原本 ...

  3. 桥接模式_NAT模式_仅主机模式_模型图.ziw

      2017年1月12日, 星期四 桥接模式_NAT模式_仅主机模式_模型图   null

  4. MySQl学习-——Mysql体系结构与Mysql存储引擎

    Mysql体系结构与Mysql存储引擎 Mysql体系结构 mysql体系结构图:

  5. 查询timestamp类型数据

    $where=" roleid = 8 and lizhi = 0 and branchid IN (".implode(",",$ids).") a ...

  6. SDUT 3918

    Description 这一天希酱又补了一卦,没想到每个人都发到了一张印有整数的牌,现在希酱想要继续占卜的话需要知道每个人手里拿的牌的整数具体是多少,但是她们却打起了哑谜.  穗乃果:我拿到的是 2 ...

  7. perl6正则 3: 行开头与结尾与多行开头,多行结尾

    ^ $ 匹配一行的开头或结尾, 可以用 ^ 或 $. > so 'abcde' ~~ /e$/ True > so 'abcdef' ~~ /e$/ False > so 'abcd ...

  8. 斐讯路由器L(联)B(壁)K-码兑换包安全下车通道(图文教程)

    大家好,最近大家比较关心的斐讯路由器如何下车问题,楼主亲自试提取了一遍,记录下过程,欢迎大家一起讨论. 言归正传,上图,上图! No.1 打开斐讯提供的良心k码退换通道: https://tech-s ...

  9. What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)

    I go to the gym and lift weights because I like the feeling of getting stronger and better. Two mont ...

  10. Nginx服务安全设置和参数调优

    1.添加参数隐藏Nginx版本号 vim /application/nginx/conf/nginx.conf #http标签下添加 server_tokens off; #测试 [root@cobb ...