Submission #448319

#TimeUsernameProblemLanguageResultExecution timeMemory
448319prvocisloT-Covering (eJOI19_covering)C++17
100 / 100
148 ms21660 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int dx[4] = { 0, -1, 0, 1 }; const int dy[4] = { -1, 0, 1, 0 }; int m, n; // pocet riadkov, pocet stlpcov vector<vector<int> > w; vector<vector<bool> > ce, vis; bool valid(int x, int y) { return x >= 0 && y >= 0 && x < m && y < n; } void dfs(int x, int y, int& c, int& f, vector<int> &v) { if (ce[x][y]) c++; else f++, v.push_back(w[x][y]); vis[x][y] = true; for (int i = 0; i < 4; i++) { int xi = x + dx[i], yi = y + dy[i]; if (valid(xi, yi) && !vis[xi][yi] && (ce[x][y] || ce[xi][yi])) dfs(xi, yi, c, f, v); } } void no() { cout << "No\n"; exit(0); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> m >> n; w.assign(m, vector<int>(n, 0)), ce.assign(m, vector<bool>(n, false)), vis.assign(m, vector<bool>(n, false)); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> w[i][j]; int k; cin >> k; int ans = 0; for (int i = 0, r, c; i < k; i++) cin >> r >> c, ce[r][c] = true, ans += w[r][c]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (ce[i][j] && !vis[i][j]) { int c = 0, f = 0; vector<int> v; dfs(i, j, c, f, v); if (c * 3 > f) no(); swap(v.back(), *min_element(v.begin(), v.end())); for (int i = 0; i < c * 3; i++) ans += v[i]; } 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...
#Verdict Execution timeMemoryGrader output
Fetching results...