// bfs while (!q.empty()) { pair<int, int> u = q.front(); q.pop(); for (int d = 0; d < 4; ++d) { pair<int, int> v = make_pair(u.first + dx[d], u.second + dy[d]); if (v.first <= 0 || v.first > n || v.second <= 0 || v.second > m) continue; if (vis[v.first][v.second]) continue; if (h[v.first][v.second] >= h[u.first][u.second]) continue; q.push(v); vis[v.first][v.second] = 1; } }
// update city cover info for (int j = 1; j <= m; ++j) { vis_last[j] |= vis[n][j]; }
// update segment info seg[s.second].first = 1; while (seg[s.second].first <= m && !vis[n][seg[s.second].first]) ++seg[s.second].first; seg[s.second].second = m; while (seg[s.second].second >= 1 && !vis[n][seg[s.second].second]) --seg[s.second].second; }
intmain(){ // read input scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf("%d", &h[i][j]); } }
// find uncovered cities int uncovered_num = 0; for (int j = 1; j <= m; ++j) { bfs(make_pair(1, j)); } for (int j = 1; j <= m; ++j) { if (!vis_last[j]) { ++uncovered_num; } } if (uncovered_num) { printf("%d\n%d", 0, uncovered_num); exit(0); }
// calculate min cover sort(seg + 1, seg + 1 + m); int construct_num = 0; int current_cover_max = 0; int next_cover_max = seg[1].second; for (int i = 1; i <= m; ++i) { if (seg[i].first > m) break; if (seg[i].first > current_cover_max + 1) { construct_num += 1; current_cover_max = next_cover_max; } next_cover_max = max(next_cover_max, seg[i].second); } if (current_cover_max < m) ++construct_num; printf("%d\n%d", 1, construct_num);