【HDU 6050 To my boyfriend】

To my boyfriend

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

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的方格,每个格子中都包含一种颜色,求出任意一个矩形包含不同颜色的期望

思路 : 从做到右从上到下,暴力统计

AC代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e2 + 10;
typedef long long LL;
LL a[MAX][MAX],n,m;
LL js(int x,int y){
    int ll = 1,rr = m;
    LL ans = 0;
    for(int i = x; i >= 1; i--){
        if(i < x && a[i][y] == a[x][y]) break;
        int l = y,r = y;
        for(int j = y - 1; j >= ll; j--)
            if(a[i][j] == a[x][y]) break;
            else l = j;
        ll = max(ll,l);
        if(i == x){
            ans += (LL)(n - x + 1) * (y - ll + 1) * (m - y + 1);
            continue;
        }
        for(int j = y + 1; j <= rr; j++)
            if(a[i][j] == a[x][y]) break;
            else r = j;
        rr = min(rr,r);
        ans += (LL)(n - x + 1) * (y - ll + 1) * (rr - y + 1);
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld %lld",&n,&m);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                scanf("%lld",&a[i][j]);
        LL ans = 0,sum = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                ans += js(i,j),sum += i * j;
        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、付费专栏及课程。

余额充值