#include<bits/stdc++.h>
#define For(i, a, b) for(int i = a, _b = b; i <= _b; ++i)
#define Ford(i, a, b) for(int i = a, _b = b; i >= _b; --i)
#define FileName "test"
#define ll long long
#define ld long double
#define ull unsigned long long
#define Print(x) cerr << #x << "is " << x << '\n';
#define pb push_back
#define X first
#define Y second
//#define Karma
using namespace std;
template<typename T> inline void Cin(T &x)
{
char c;
T sign = 1;
x = 0;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
const int N = 5e3 + 7;
int x, n, m, res, Min;
vector<vector<int>> a;
int Sum(int x, int y, int u, int v)
{
return a[x - 1][y - 1] + a[u][v] - a[x - 1][v] - a[u][y - 1];
}
void Enter()
{
Cin(n, m);
a.resize(n + 1);
for(int i = 0; i <= n; ++i) a[i].resize(m + 1, 0);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
Cin(x);
if(x) ++ res, x = -1;
else x = 1;
a[i][j] = a[i][j - 1] + a[i - 1][j] + x - a[i - 1][j - 1];
}
}
}
/*
5 7
0 0 1 0 0 1 0
0 1 1 1 1 1 0
0 1 1 0 0 1 0
0 1 1 1 1 1 0
0 0 1 0 0 1 0
*/
void Solve()
{
Min = n * m;
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) {
int Pre, s;
Min = min(Min, s = Sum(i, 1, j, 1));
Pre = max(s, 0);
for(int k = 2; k <= m; ++k) {
s += Sum(i, k, j, k);
//cout << i << ' ' << j << ' ' << k << ' ' << s << ' ' << Pre << '\n';
Min = min(Min, s - Pre);
Pre = max(Pre, s);
}
}
}
cout << res + Min;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef Karma
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
#endif // Karma
Enter();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
10128 KB |
Output is correct |
2 |
Correct |
33 ms |
10152 KB |
Output is correct |
3 |
Correct |
34 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1916 KB |
Output is correct |
2 |
Correct |
9 ms |
1912 KB |
Output is correct |
3 |
Correct |
8 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
9 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
4728 KB |
Output is correct |
2 |
Correct |
213 ms |
4828 KB |
Output is correct |
3 |
Correct |
212 ms |
4788 KB |
Output is correct |