[Reserved] $ CF57D $ explanations

Read the original

Given a \ (n \ times m \) of the trellis diagram, where there is an obstacle, per row and no more than one, each of the barrier around \ (8 \) format accessible. Seeking a desired length of a path between two optional points accessible.

Consider push path length of the desired formula: Let accessibility point of "good point", there are obstacles to point to "pixel", two different "good points" \ (i \) and \ (j \) between distance \ (D (I, J) \) . "Good point," a total of \ (the p-\) . The desired path length \ (= \ FRAC {\ sum_ = {I}. 1 ^ P \ sum_ {J} = ^ PD. 1 (I, J)} {P} ^ 2 \) .

Then push the two different "good points" between distance: For most of the "good points" distance between two points is its Manhattan distance. For the following figure \ (A \) point and \ (B \) point ( "better" counterparts have the right to the left "pixel" \ (1 \) left, "good points" have the right counterparts "pixel" \ (2 \) and "pixel" \ (1 \) in the "pixel" \ (2 \) on the left), then the two "better" Manhattan distance for spacing \ (2 + \) .

The special circumstances of distance between "good points" Figure twelve

Manhattan distance is then calculated: Consider branches and columns do respectively.

To conduct cases, the number of pieces held \ (A \) , for the "good points" for each row overall distance traveled in the vertical direction is calculated. Below, for a "better" \ ((A, B) \) , which is the first \ (C \) distance for all points on the line and equal to the vertical \ (C \) all "good dot lines "number \ (\ Times \) of \ (C \) row and the second \ (a \) distance between rows difference.

Known by the title per line \ (m \) points, then the first \ (c \) when the line of "dead pixels", answers contributed \ ((m-1) \ ABS Times (CA) \) (following FIG \ (Case \) \ (. 1 \) ); section \ (C \) when the line is not "pixel", answers contributed \ (m \ ABS Times (CA) \) (below \ (Case \) \ (2 \) ).

Figure II a "good spot" in a certain direction and the distance between the line "good points" I.

Figure III a "good spot" and upward from line "good points" between the case where either one of two

Finally, consider the Manhattan distance \ (+ 2 \) "good point" how to make up \ (2 \) detour: Before most people have heard of the practice repeat, I do not here speak a heavy!

Or branches and columns consider: the behavior of patients, the number of pieces held \ (A \) . When the first \ (a \) when the line of "dead pixels", the "pixel" All points are likely to detour left.

All check rows down, maintenance (as FIG blue) such that one block in the block of all "good points" are required detour:

Maintaining downward, as long as the line current "pixel" to the left "pixel" in line below it, all of the points can be "pixel" is added to the right side of the block;

If the current line of "dead pixels" on the right "dead pixels" in its top row, then all the "good points" "pixel" on the right end of the maintenance (such as red "better" after joining in the block are not required added to the block). Development of a lower edge of an entire block to the right.

Similarly maintenance upward, the entire development of the lower edge of the block to the right. After both the upper and lower ends, provided the total number of blue point \ (Q \) , for the first \ (A \) a dot row \ ((a, b) \ ) , it blocks into any point of the detour are is \ (2 \ Times Q \) .

This time is calculated from the upper left to lower right path "good points" and from the lower left to the upper right of the "good points". At this time, just as long as the calculated value \ (\ Times 2 \) , will come to the "good points" need to detour all the "good points" and all need to detour to the "good points" to "good point "and the long detour.

After the calculated value, take the first \ (a \) the number of "good point" OK "dead pixels" to the left of the answer can be added.

图四 两“好点”间距离的特殊情况

After each row of the following operations once finished, enumeration column can do a similar operation.

The whole process is so, if not understand or feel the need to prove, see the comments section! Thank you for your patience to read!

code show as below:

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int n,m,totx,tot,hangx[2005],liex[2005];
double ans;
char s[2005];
inline long long finds(int n,int m,int a[])
{
    long long ret=0;
    for(register int i=1;i<=n;i++)
    {
        long long sum=0,cnt=m-a[i];
        for(register int j=1;j<=n;j++)
        {
            if(a[j])
                sum+=(m-1)*abs(i-j);
            else
                sum+=m*abs(i-j);
        }
        if(a[i])
            ret+=(m-1)*sum;
        else
            ret+=m*sum;
        if(a[i])
        {
            int l=i-1,r=i+1;
            while(a[l+1]<a[l])
            {
                cnt+=m-a[l];
                l--;
            }
            while(a[r-1]<a[r])
            {
                cnt+=m-a[r];
                r++;
            }
            ret+=4*cnt*(a[i]-1);
        }
    }
    return ret;
}
int main()
{
    n=read();
    m=read();
    for(register int i=1;i<=n;i++)
    {
        scanf("%s",&s);
        for(register int j=1;j<=m;j++)
        {
            if(s[j-1]=='X')
            {
                hangx[i]=j;
                liex[j]=i;
                totx++;
            }
        }
    }
    tot=n*m-totx;
    ans=(finds(n,m,hangx)+finds(m,n,liex))*1.0/tot/tot;
    printf("%0.6lf\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Peter0701/p/11309122.html
Recommended