Submission #477865

# Submission time Handle Problem Language Result Execution time Memory
477865 2021-10-04T08:59:50 Z BThero Orchard (NOI14_orchard) C++17
25 / 25
341 ms 17916 KB
#include <bits/stdc++.h>

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

using namespace std;

typedef long long ll;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m, 0));
	vector<vector<int>> p(n + 1, vector<int>(m + 1, 0));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
		}
	}

	int ans = (int)1e9;
	int cnt = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cnt += a[i][j];

			if (a[i][j] == 1) {
				a[i][j] = -1;
			}
			else {
				a[i][j] = 1;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			p[i + 1][j + 1] = a[i][j]
			+ p[i][j + 1]
			+ p[i + 1][j]
			- p[i][j];
		}
	}

	for (int x1 = 0; x1 < n; x1++) {
		for (int x2 = x1 + 1; x2 <= n; x2++) {
			vector<int> T(m + 1, 0);
			T[m] = p[x2][m] - p[x1][m];

			for (int y = m - 1; y >= 0; y--) {
				T[y] = min(T[y + 1], p[x2][y] - p[x1][y]);
			}
		
			for (int y = 0; y < m; y++) {
				int cur = p[x1][y] - p[x2][y] + T[y + 1];
				ans = min(ans, cnt + cur);
			}
		}
	}

	cout << ans << '\n';
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 15880 KB Output is correct
2 Correct 82 ms 17916 KB Output is correct
3 Correct 76 ms 17880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2676 KB Output is correct
2 Correct 18 ms 3076 KB Output is correct
3 Correct 15 ms 3016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 11 ms 500 KB Output is correct
3 Correct 12 ms 496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 333 ms 6220 KB Output is correct
2 Correct 341 ms 7684 KB Output is correct
3 Correct 338 ms 7692 KB Output is correct