UVa 11951

first the 2-D array is row-major compressed to 1-D, which means each time more row's columns are iterated at the same time, pointer h is used to calculate area
j-i+1 is the total rows being iterated now
k-h+1 is the maximum column number that satisfy the limit
/* UVas */
/*****************************************************************************/
#define ll long long
int t, N, M, ce=1;
ll K;
ll mat2[100][100];
ll mat1[100];
int main(void) {
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d %lld", &N, &M, &K);
        for (int i=0; i<N; i++)
            for (int j=0; j<M; j++)
                scanf("%lld", &mat2[i][j]);
        ll r0=0, r1=0;
        for (int i=0; i<N; i++) {
            memset(mat1, 0, sizeof(mat1));
            for (int j=i; j<N; j++) {
                for (int k=0; k<M; k++)
                    mat1[k]+=mat2[j][k];
                int h=0;
                ll c=0;
                for (int k=0; k<M; k++) {
                    c+=mat1[k];
                    while (c>K)
                        c-=mat1[h++];
                    ll a=(j-i+1)*(k-h+1);
                    if (a>r0) {
                        r0=a;
                        r1=c;
                    } else if (a==r0) {
                        r1=min(r1, c);
                    }
                }
            }
        }
        printf("Case #%d: %lld %lld\n", ce++, r0, r1);
    }
}
/*****************************************************************************/

No comments :

Post a Comment