#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
void solve() {
int n, m; cin >> n >> m;
int sum = 0, ps[n + 1][m + 1] = {};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c; cin >> c;
int d = c == '1' ? 1 : -1;
ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + d;
sum += c == '1';
}
}
int ans = 0; // rectangle with maximum score
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
int mn = 0;
for (int k = 1; k <= m; k++) {
int cur = ps[j][k] - ps[i - 1][k];
ans = max(ans, cur - mn);
mn = min(mn, cur);
}
}
}
cout << sum - ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
8172 KB |
Output is correct |
2 |
Correct |
29 ms |
8172 KB |
Output is correct |
3 |
Correct |
36 ms |
8172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1516 KB |
Output is correct |
2 |
Correct |
6 ms |
1516 KB |
Output is correct |
3 |
Correct |
6 ms |
1516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
364 KB |
Output is correct |
3 |
Correct |
4 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
3308 KB |
Output is correct |
2 |
Correct |
118 ms |
3584 KB |
Output is correct |
3 |
Correct |
107 ms |
3436 KB |
Output is correct |