您的位置:首页 > 其它

【HDU 6050 To my boyfriend】

2017-07-29 19:23 155 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: