#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e3 + 2;
vector<vector<int>>a,pre;
vector<int>sum,suf;
int n,m,total;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
total = 0;
a.assign(n + 5,vector<int>(m + 5));
pre.assign(n + 5,vector<int>(m + 5,0));
sum.assign(m + 5,0);
suf.assign(m + 5,0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
char x;
cin >> x;
a[i][j] = x - '0';
total += a[i][j];
pre[i][j] = pre[i - 1][j] + 1 - a[i][j];
}
int ans = 1e18;
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++) {
sum[0] = 0;
for (int i = 1; i <= m; i++)
sum[i] = sum[i - 1] + pre[r][i] - pre[l - 1][i];
suf[m + 1] = 1e18;
for (int i = m; i > 0; i--)
suf[i] = min(suf[i + 1],2 * sum[i] - (r - l + 1) * i);
for (int i = 1; i <= m; i++)
ans = min(ans,suf[i + 1] - 2 * sum[i - 1] + total + (r - l + 1) * (i - 1));
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
2 ms |
1912 KB |
Output is correct |
3 |
Correct |
2 ms |
1872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
110016 KB |
Output is correct |
2 |
Correct |
87 ms |
110020 KB |
Output is correct |
3 |
Correct |
88 ms |
110008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
12864 KB |
Output is correct |
2 |
Correct |
13 ms |
12864 KB |
Output is correct |
3 |
Correct |
13 ms |
12864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
592 KB |
Output is correct |
2 |
Correct |
5 ms |
592 KB |
Output is correct |
3 |
Correct |
6 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
12624 KB |
Output is correct |
2 |
Correct |
170 ms |
14160 KB |
Output is correct |
3 |
Correct |
169 ms |
14160 KB |
Output is correct |