您的位置:首页 > 其它

HDU-6052 To my boyfriend 思维

2017-07-29 15:47 447 查看
传送门


To my boyfriend

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 621    Accepted Submission(s): 284


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

2017 Multi-University Training Contest - Team 2

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6055 6054 6053 6052 6051 

 

题意:对于一个n*m的方格,每个格子中都包含一种颜色,求出任意一个矩形包含不同颜色的期望。

这个题得考虑每一个有颜色的点对整个答案有什么贡献。为了避免重复计算这一种颜色的点,(因为一种颜色可能会有多个方格,
在计算某个方格的贡献时,与它相同颜色且被计算过的方格是不能考虑的),这时可以把这些方格当做障碍物。所以可以指定一个
方向算点对答案的贡献。规定一个计算的顺序,比如按照从上到下,从左到右的顺序给每个同色的点排个序,计算这个颜色的贡
献时,对于第i个点,可以计算包括点i以及包括之后所有同色点的矩形数目,但是不能计算包括i之前的点的矩形数目,这样就可
以不重复的计算。具体处理就是处理上边界,左边界,右边界。然后计算方案数就完了。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e2+10;
const int maxx=1e4+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline int Scan()
{
int Res=0,ch,Flag=0;
if((ch=getchar())=='-')Flag=1;
else if(ch>='0' && ch<='9')Res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int n,m,a[maxn][maxn];
//(下边界方案*左边界方案*右边界方案,上边界已经确定是xi)
double solve(int x,int y)
{
double res=0;
int c=a[x][y],L=1,R=m;
for(int i=x;i>=1;i--)//上边界
{
if(i<x&&a[i][y]==c) break; // 同一列
int l=y,r=y;
for(int j=y-1;j>=max(1,L);j--)//左边界
{
if(a[i][j]==c) break;
l=j;
}
L=max(l,L);
if(i==x)
{
res+=(LL)(n-x+1LL)*(y-L+1LL)*(R-y+1LL);
continue;
}
for(int j=y+1;j<=min(m,R);j++)//右边界
{
if(a[i][j]==c) break;
r=j;
}
R=min(R,r);
res+=(LL)(n-x+1LL)*(y-L+1LL)*(R-y+1LL);
}
return res;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
double ans=0,mx=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
ans+=solve(i,j);
mx+=i*j;
}
printf("%.9lf\n",ans/mx);
}
//cerr << "run time is " << clock() << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: