#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long seed;
asm("rdtsc" : "=A"(seed));
srand(seed);
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n+1, vector<int>(m+1, 0));
vector<vector<int>> zs(n+1, vector<int>(m+1, 0));
vector<vector<int>> os(n+1, vector<int>(m+1, 0));
int totalOnes = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> grid[i][j];
if(grid[i][j])
totalOnes++;
os[i][j] = grid[i][j] + os[i-1][j] + os[i][j-1] - os[i-1][j-1];
zs[i][j] =!grid[i][j] + zs[i-1][j] + zs[i][j-1] - zs[i-1][j-1];
}
}
int ans = 1ll<<29;
for(int top = 0; top < n; top++) {
for(int bot = top+1; bot <= n; bot++) {
int carried = totalOnes;
for(int j = 1; j <= m; j++) {
carried -= os[bot][j] - os[bot][j-1] - os[top][j] + os[top][j-1];
carried += zs[bot][j] - zs[bot][j-1] - zs[top][j] + zs[top][j-1];
int single = (zs[bot][j] - zs[bot][j-1] - zs[top][j] + zs[top][j-1]) + (totalOnes - (os[bot][j] - os[bot][j-1] - os[top][j] + os[top][j-1]));
carried = min(carried, single);
ans = min(ans, carried);
}
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1216 KB |
Output is correct |
2 |
Correct |
5 ms |
1216 KB |
Output is correct |
3 |
Correct |
5 ms |
1344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
55248 KB |
Output is correct |
2 |
Correct |
133 ms |
55300 KB |
Output is correct |
3 |
Correct |
155 ms |
55324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
55324 KB |
Output is correct |
2 |
Correct |
24 ms |
55324 KB |
Output is correct |
3 |
Correct |
24 ms |
55324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
55324 KB |
Output is correct |
2 |
Correct |
11 ms |
55324 KB |
Output is correct |
3 |
Correct |
11 ms |
55324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
55324 KB |
Output is correct |
2 |
Correct |
348 ms |
55324 KB |
Output is correct |
3 |
Correct |
271 ms |
55324 KB |
Output is correct |