This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |