Submission #477865

#TimeUsernameProblemLanguageResultExecution timeMemory
477865BTheroOrchard (NOI14_orchard)C++17
25 / 25
341 ms17916 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...