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<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;
}
# | 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... |