#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n,m,fcnt;
int* a[155];
int* s[155];
int ca[1000005];
int sum(){
int me = *max_element(ca+1,ca+m+1);
if(me < 0) return -me;
int mv = 0, c = 0;
for (int j=1; j<=m; j++) {
c += ca[j];
if(c < 0) c = 0;
mv = max(mv,c);
}
return -mv;
}
int calc(int x, int y){
int hi = y - x + 1;
for (int j=1; j<=m; j++) {
ca[j] = 2 * (s[y][j] - s[y][j-1] - s[x-1][j] + s[x-1][j-1]) - hi;
}
return fcnt + sum();
}
int main(){
scanf("%d %d",&n,&m);
for (int i=0; i<=n; i++) {
a[i] = (int*)malloc(sizeof(int) * (m + 1));
s[i] = (int*)malloc(sizeof(int) * (m + 1));
}
for (int i=0; i<=m; i++) {
s[0][i] = 0;
}
for (int i=0; i<=n; i++) {
s[i][0] = 0;
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
scanf("%d",&a[i][j]);
s[i][j] = s[i][j-1] + s[i-1][j] - s[i-1][j-1] + a[i][j];
}
}
fcnt = s[n][m];
int res = 1e9;
for (int i=1; i<=n; i++) {
for (int j=i; j<=n; j++) {
res = min(res,calc(i,j));
}
}
printf("%d",res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4996 KB |
Output is correct |
2 |
Correct |
0 ms |
4996 KB |
Output is correct |
3 |
Correct |
0 ms |
4996 KB |
Output is correct |
4 |
Correct |
0 ms |
4996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5172 KB |
Output is correct |
2 |
Correct |
0 ms |
5172 KB |
Output is correct |
3 |
Correct |
0 ms |
5172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
20628 KB |
Output is correct |
2 |
Correct |
104 ms |
20628 KB |
Output is correct |
3 |
Correct |
120 ms |
20628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
7348 KB |
Output is correct |
2 |
Correct |
28 ms |
7348 KB |
Output is correct |
3 |
Correct |
20 ms |
7348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4996 KB |
Output is correct |
2 |
Correct |
12 ms |
5128 KB |
Output is correct |
3 |
Correct |
8 ms |
5128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
10880 KB |
Output is correct |
2 |
Correct |
376 ms |
10880 KB |
Output is correct |
3 |
Correct |
360 ms |
10880 KB |
Output is correct |