2017多校第二场 HDU 6052 To my boyfriend 思维,计数题

来源:互联网 发布:四层横移编程 编辑:程序博客网 时间:2024/06/16 05:41

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6052
题意:对于一个n*m的方格,每个格子中都包含一种颜色,求出任意一个矩形包含不同颜色的期望。
解法:题解描述来源:http://blog.csdn.net/bahuia/article/details/76283856
算贡献,计数问题,这种可能会重复的计数,为了避免重复,需要规定一个计算的顺序,比如按照从上到下,从左到右的顺序给每个同色的点排个序,计算这个颜色的贡献时,对于第i个点,可以计算包括点i以及包括之后所有同色点的矩形数目,但是不能计算包括i之前的点的矩形数目,这样就可以不重复的计算。如果按照从上到下,从左到右的顺序计算,那么对于当前点x,和它同色且在他上方和左边的点都不能包括。
这样问题就转化为,对于每个点,我们要求在当前限制点的条件下,能覆盖这个点,但不能覆盖限制点的最大矩形的上下左右边界。首先枚举上边界,同时维护左右边界的值,对于每个枚举的上边界,更新左右边界的值,然后计算在固定上边界的情况下,有多少个矩形可以覆盖x点(i,j),若此时左右边界分别是L,R,那么包括x的点的矩形数目为(j-L+1)(R-j+1)(n-i+1)。
复杂度 理论O(n^4)

//HDU 6052 暴力577MS  #include <bits/stdc++.h>using namespace std;typedef long long LL;int n, m, col[105][105];int cal(int x, int y){    LL ans = 0;    int c = col[x][y], L = 1, R = m;    for(int i=x; i>=1; i--){        if(i<x&&col[i][y]==c) break;        int l = y, r = y;        for(int j=y-1; j>=max(1,L); j--){            if(col[i][j]==c) break;            l=j;        }        L=max(L, l);        if(i==x){            ans += 1LL*(n-x+1)*(y-L+1)*(R-y+1);            continue;        }        for(int j=y+1; j<=min(m, R); j++){            if(col[i][j]==c) break;            r=j;        }        R=min(R,r);        ans+=1LL*(n-x+1)*(y-L+1)*(R-y+1);    }    return ans;}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", &col[i][j]);            }        }        LL sum1 = 0, sum2 = 0;        for( int i=1; i<=n; i++){            for(int j=1; j<=m; j++){                sum1 += cal(i, j);                sum2 += i*j;            }        }        printf("%.9f\n", 1.0*sum1/sum2);    }    return 0;}

优化:我们对于一种颜色,只需要考虑最下方的那个对应颜色的点,这个证明很显然,因为你往上走r会不断变小,l会不断增大。所以我们跑到最下面那个点就足够了。

时间复杂度:O(n^3)

//78MS#include <bits/stdc++.h>using namespace std;typedef long long LL;struct FastIO{    static const int S = 1310720;    int wpos;    char wbuf[S];    FastIO() : wpos(0) {}    inline int xchar()    {        static char buf[S];        static int len = 0, pos = 0;        if (pos == len)            pos = 0, len = fread(buf, 1, S, stdin);        if (pos == len) return -1;        return buf[pos ++];    }    inline int xuint()    {        int c = xchar(), x = 0;        while (c <= 32) c = xchar();        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';        return x;    }    inline int xint()    {        int s = 1, c = xchar(), x = 0;        while (c <= 32) c = xchar();        if (c == '-') s = -1, c = xchar();        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';        return x * s;    }    inline void xstring(char *s)    {        int c = xchar();        while (c <= 32) c = xchar();        for (; c > 32; c = xchar()) * s++ = c;        *s = 0;    }    inline void wchar(int x)    {        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;        wbuf[wpos ++] = x;    }    inline void wint(LL x)    {        if (x < 0) wchar('-'), x = -x;        char s[24];        int n = 0;        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;        while (n--) wchar(s[n]);    }    inline void wstring(const char *s)    {        while (*s) wchar(*s++);    }    ~FastIO()    {        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;    }} io;const int maxn = 101;int T,n,m,mp[maxn][maxn];vector <pair<int,int>> col[maxn*maxn];int bo[maxn];vector <int> y[maxn];LL cal(int c){    LL ans = 0;    memset(bo, 0, sizeof(bo));    for(int i=0; i<maxn; i++){        y[i].clear();    }    for(auto now:col[c]){        int l = now.first, r = now.second;        for(int i=1; i<=m; i++) y[i].clear();        for(int i=1; i<=m; i++){            if(bo[i]){                y[bo[i]].push_back(i);            }        }        int L = 1, R = m;        bool flag = 0;        for(int i = l; i>=1; i--){            for(vector<int>::iterator it = y[i].begin(); it!=y[i].end(); it++){                if(*it<r){                    L=max(L,*it+1);                }                else if(*it>r){                    R=min(R,*it-1);                }                else{                    flag = 1;                    break;                }            }            if(flag) break;            ans += 1LL*(n-l+1)*(r-L+1)*(R-r+1);        }        bo[r] = l;    }    return ans;}int main(){    T = io.xint();    while(T--)    {        //scanf("%d %d", &n,&m);        n = io.xint(), m = io.xint();        for(int i=1; i<=n; i++){            for(int j=1; j<=m; j++){                mp[i][j] = io.xint();            }        }        for(int i=0; i<=n*m; i++) col[i].clear();        for(int i=1; i<=n; i++){            for(int j=1; j<=m; j++){                col[mp[i][j]].push_back(make_pair(i,j));            }        }        for(int i=0; i<n*m; i++){            if(!col[i].empty()){                sort(col[i].begin(),col[i].end());            }        }        LL sum1 = 0, sum2 = 0;        for(int i=1; i<=n; i++){            for(int j=1; j<=m; j++){                sum2 += i*j;            }        }        for(int i=0; i<n*m; i++){            if(!col[i].empty()){                sum1 += cal(i);            }        }        printf("%.9f\n", 1.0*sum1/sum2);    }    return 0;}
原创粉丝点击