HDU 6052

题意略。

思路:我们单独考虑每种颜色的贡献,颜色c对答案的贡献 == 含有c的矩形个数,这就是在考查我们计数的能力,暴力可过。

#include<bits/stdc++.h>
#define maxn 105
using namespace std;

int color[maxn][maxn];
int n,m;

double calcu(int x,int y){
    int l = 0,r = m + 1,c = color[x][y];
    double ret = 0;
    for(int i = x;i >= 1;--i){
        if(color[i][y] == c && i < x) break;
        for(int j = y - 1;j >= 1;--j){
            if(color[i][j] == c){
                l = max(l,j);
                break;
            }
        }
        if(i == x){
            ret += max(1,(y - l)) * (m - y + 1) * (n - x + 1);
            continue;
        }
        for(int j = y + 1;j <= m;++j){
            if(color[i][j] == c){
                r = min(r,j);
                break;
            }
        }
        //printf("l == %d r == %d
",l,r);
        ret += max(1,(y - l)) * max(1,(r - y)) * (n - x + 1);
    }
    //printf("now ret == %lf
",ret);
    return ret;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;++i){
            for(int j = 1;j <= m;++j){
                scanf("%d",&color[i][j]);
            }
        }
        double numerator = 0,denominator = 0;
        for(int i = 1;i <= n;++i){
            for(int j = 1;j <= m;++j){
                denominator += i * j;
                numerator += calcu(i,j);
            }
        }
        printf("%.9lf
",numerator / denominator);
    }
    return 0;
}

/*
1
3 3
1 2 3
4 5 6
7 8 9
*/
原文地址:https://www.cnblogs.com/tiberius/p/8597175.html