hdu6052To my boyfriend(子矩阵计数处理)

To my boyfriend

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


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)
计算子矩阵颜色期望值,每个矩阵数字代表一个颜色最多有10000种,也就是不同颜色子矩阵数量/子矩阵
首先考虑子矩阵数量,子矩阵可以枚举两个i~n,j~m,子矩阵为i*j的数量。
然后考虑颜色,对于每个颜色单独考虑,也就是每个点要单独考虑,将点作为关键值考虑,每次计算的矩阵至少要包括这个点
然后就是考虑子矩阵的上下左右域了,对于每个颜色点,先用row[c][j]数组,代表颜色为c,第j列的行号。接下来考虑点的value时下域就不要考虑了,直接是n-x+1,上域有不同,如果每个点向上出现了已经标记过的点,那么上域到此行结束,左右域也是取决于是否有标记过的点出现在和上域相应的列上,否则到此结束。在这里左右域有个优化,如果点是当前列最后一行,那么不用考虑上面的点了,因为上面的点的左右域只会小于。
这样计算出每个点的左右下域,就能直接算出子矩阵的不同矩阵颜色值=左*右*下=(y-l[i])*(r[i]-y)*(n-x+1),再求出所有的点的值就是
子矩阵的不同颜色数量。
计算过程:先对每个点的前x个点的左右域初始化为0和m+1(最大化),然后对前y点的左域和后y点的右域更新,每个点的上域肯定是更新后的最后一个点,也就是第y列的点的行号确认。 最后再从x-1到h行对点的左右域进行拉伸,找到公共的左右域,这样就找出来所有的域
参考:http://www.cnblogs.com/nicetomeetu/p/7261147.html
http://blog.csdn.net/calabash_boy/article/details/76272704?yyue=a21bo.50862.201879
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
int l[110],r[110],row[10010][110];
int map[110][110];
int n,m;
long long ans;
void cal(int x,int y,int c)
{
    for(int i=1;i<=x;i++)
    {
        l[i]=0;
        r[i]=m+1;
    }
    for(int i=1;i<y;i++)
    {
        l[row[c][i]]=i;
    }
    for(int i=m;i>y;i--)
    {
        r[row[c][i]]=i;
    }
    int h=row[c][y];
    for(int i=x-1;i>h;i--)
    {
        l[i]=max(l[i],l[i+1]);
        r[i]=min(r[i],r[i+1]);
    }
    for(int i=x;i>h;i--)
    {
        ans+=(long long)(y-l[i])*(r[i]-y)*(n-x+1);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    //int c;
    int c;
    while(t--)
    {
        memset(row,0,sizeof(row));
        scanf("%d%d",&n,&m);
        ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&c);
                cal(i,j,c);
                row[c][j]=i;
            }
        }
        long long sum=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                sum+=i*j;
            }
        }
        //cout<<ans<<endl;
        //cout<<sum<<endl;
        printf("%.9f\n",(double)ans/sum);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值