UVa 10755

kadane's algorithm on 3-dimension array
origin array is compressed 2 times to 1-dimension
/* UVas */
/*****************************************************************************/
int t, a, b, c;
long long d3[21][21][21];
long long d2[21][21];
long long d1[21];
int main(void) {
    cin>>t;
    while (t--) {
        cin>>a>>b>>c;
        for (int i=0; i<a; i++)
            for (int j=0; j<b; j++)
                for (int k=0; k<c; k++) cin>>d3[i][j][k];
        long long r=INT_MIN;
        for (int i=0; i<a; i++) { // first compression
            memset(d2, 0, sizeof(d2));
            for (int ii=i; ii<a; ii++) {
                for (int j=0; j<b; j++)
                    for (int k=0; k<c; k++) d2[j][k]+=d3[ii][j][k];
                for (int i=0; i<b; i++) { // second compression
                    memset(d1, 0, sizeof(d1));
                    for (int j=i; j<b; j++) {
                        for (int k=0; k<c; k++) d1[k]+=d2[j][k];
                        long long n=d1[0];
                        r=max(r, n);
                        for (int k=1; k<c; k++) { 
                            n=max(n, 0LL);
                            n+=d1[k];
                            r=max(r, n);
                        }
                    }
                }
            }
        }
        cout<<r<<endl;
        if (t) cout<<endl;
    }
}
/*****************************************************************************/

No comments :

Post a Comment