THOI 2012 Water Level

Source: Internet
Author: User
A1363. Water level

Thinking questions.

When doing this problem, if the idea is clear, it is a simple multiplication principle + high-precision problem.

Sort Ascending by original height

At the very beginning, all points belong to one connected block respectively.

According to the height sequence, from the lowest beginning to merge the connected block, assuming the current processing to l−r L-r this interval, their height is HL h_l first remember two points
Before merging them, each point in the connected block is as high as the merging of them without affecting how the parts after r R are merged.

We enumerate four directions, if the enumerated point Y Y and the current point x X are not in a connected block, judging if they are the same height, if the same, the number of schemes of x x is multiplied by the scheme number of Y y points ans[y] ans[y] If different, y y this connected block can also raise the Hx−hy H_ X-h_y, at this time ans[x]∗= (ans[y]+hx−hy) ans[x]*= (ans[y]+h_x-h_y)

So after this merger, the connected block height of the merge is HL H_l and then continues the process, knowing that there is a connected block

This is done directly with 45 points, plus high accuracy on AC ~

#include <iostream> #include <algorithm> #include <cmath> #include <cstring> #include <
cstdio> #include <cstdlib> #define M 10005 #define LL Long long #define MOD 10000 using namespace std;
int n,m;
    struct Bign {int l,x[5005];
        Bign operator = (int k) {x[1]=k;
        l=1; 
    return *this;
            } void Rebuild () {for (int i=1;i<=l;i++) {x[i+1]=x[i+1]+x[i]/mod;
        X[i]%=mod;
    } while (x[l+1]>0) L++,x[l+1]+=x[l]/mod,x[l]%=mod;
        } Friend Bign operator + (bign a,int k) {a.x[1]+=k;
        A.rebuild ();
    return A;
        } Friend bign operator * (bign a,bign b) {bign C;
        C.L=A.L+B.L-1;
        memset (c.x,0,sizeof (c.x)); for (int. i=1;i<=a.l;i++) for (int j=1;j<=b.l;j++) C.X[I+J-1]+=A.X[I]*B.X[J]%MOD,C.X[I+J]
        +=a.x[i]*b.x[j]/mod;
        C.rebuild (); return C;
    } bign operator *= (bign k) {*this=*this * k;
    return *this;
}}ans[m]; struct data {int x,y,h;}
A[M];
int fx[5][3],add[m],v[m],fa[m],dy[m];
    void Print (Bign a) {printf ("%d", a.x[a.l]);
    for (int i=a.l-1;i;i--) printf ("%04d", A.x[i]);
printf ("\ n");
    } int C (int x,int y) {return (x-1) *n+y;} int read () {int tmp=0;
    Char Ch=getchar ();
    int fu=1; for (;ch< ' 0 ' | |
    Ch> ' 9 '; Ch=getchar ()) if (ch== '-') fu=-1;
    for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) tmp=tmp*10+ch-' 0 ';
return TMP*FU;
} BOOL CMP (data A,data b) {return a.h<b.h;} int getfather (int x) {return x==fa[x]?x:fa[x]=getfather (fa[x]);
    } int ok (int x,int y) {if (x>0&&y>0&&x<=n&&y<=n) return 1;
return 0;
    } int main () {scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) a[c (i,j)]= (data) {I,j,read ()}, Fa[c (i,j)]=c (I,J), Ans[c (i,j)]=1;
    Sort (a+1,a+1+n*n,cmp);
    for (int i=1;i<=n*n;i++) dy[c (A[I].X,A[I].Y)]=i;
    A[n*n+1].h=m,a[0].h=a[1].h;
    fx[0][1]=fx[1][1]=fx[2][2]=fx[3][2]=0;
    Fx[0][2]=fx[2][1]=1,fx[1][2]=fx[3][1]=-1;
    int l=1,r=0;
        while (l<=n*n) {r=l;
        while (a[r].h==a[r+1].h&&r<n*n) r++;
            for (int i=l;i<=r;i++) {int c=c (A[I].X,A[I].Y);
            V[c]=1;
                for (int k=0;k<4;k++) {int nx=a[i].x+fx[k][1],ny=a[i].y+fx[k][2];
                int cc=c (NX,NY); if (!ok (nx,ny) | |!
                V[CC]) continue;
                int F1=getfather (CC);
                    if (f1!=c) {if (a[dy[f1]].h==a[i].h) ANS[C]*=ANS[F1];
                    Else ans[c]*= (ans[f1]+ (a[i].h-a[dy[f1]].h));
                Fa[f1]=c;
    }}} l=r+1;
    }int x;
    for (int i=1;i<=n*n;i++) if (fa[i]==i) x=i;
    Print (ans[x]+ (m-a[n*n].h));
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.