Split Matrix
(Browine. c/cpp/pas)
[Problem description]
A non-negative integer matrix with N * M. Now we need to divide the matrix into A * B blocks. Matrix first horizontally cut the A-1 knife, the matrix is divided into A block. And then the remaining parts of the independent Cutting Vertical B-1 knife. The value of each block is the number and value of each block. Find a solution to maximize the value of the smallest value block.
[Input format]
The first line contains four integers, N, M, A, and B.
Next N rows, M non-negative integers in each row. Represents this matrix
[Output format]
A number. The value of the smallest value block.
[Example input]
5 4 4 2
1 2 21
3 1 1 1
2 0 1 3
1 1 1 1
1 1 11
[Sample output]
3
For an example, see the image.
[Data scale]
1 <= A <= N <= 500
1 <= B <= M <= 500
Other numbers are less than 4000.
Please note that
M = (l + r)/2 <----- This sentence is never used by L and R.
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <cstdlib>
# Include <iostream>
# Include <functional>
# Include <algorithm>
Using namespace std;
# Define MAXN (500 + 10)
# Define MAXM (500 + 10)
# Define MAXT (2000000 + 10)
Int n, m, t1, t2, a [MAXN] [MAXM], sum [MAXN] [MAXM] = {0 };
Bool is_ OK (int l, int r, int _ m)
{
Int tot = 0, p = 0;
For (int I = 1; I <= m; I ++)
{
Tot + = sum [r] [I]-sum L-1] [I];
If (tot> = _ m) {tot = 0; p ++ ;}
}
If (p> = t2) return 1;
Else return 0;
}
Bool is_ OK _ (int _ m)
{
Int p = 0, l = 1;
For (int I = 1; I <= n; I ++)
{
If (is_ OK (l, I, _ m) {l = I + 1; p ++ ;}
}
If (p> = t1) return 1;
Else return 0;
}
Int main ()
{
Freopen ("browine. in", "r", stdin );
Freopen ("browine. out", "w", stdout );
Scanf ("% d", & n, & m, & t1, & t2 );
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
{
Scanf ("% d", & a [I] [j]);
Sum [I] [j] = sum [I-1] [j] + a [I] [j];
}
/*
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
{
Printf ("% d", sum [I] [j]);
}
*/
// Cout <(is_ OK _ (4 ));
Int l = 1, r = 1, ans = 0;
For (int j = 1; j <= m; j ++) r + = sum [n] [j];
For (int I = 1; I <= 60; I ++)
{
Int m _ = (l + r)/2;
If (is_ OK _ (m _) {l = ans = m _;}
Else r = m _;
}
Printf ("% d \ n", ans );
// While (1 );
Return 0;
}