比赛 2025暑期集训第8场 评测结果 AATAAAAAAA
题目名称 引水入城 最终得分 90
用户昵称 对立猫猫对立 运行时间 1.289 s
代码语言 C++ 内存使用 3.93 MiB
提交时间 2025-08-13 11:46:51
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct Interval {
	int left;
	int right;
	bool operator<(const Interval& other) const {
		return left < other.left;
	}
};
int main() {
	freopen("flow.in", "r", stdin);
	freopen("flow.out", "w", stdout);
	int N, M;
	cin >> N >> M;
	vector<vector<int>> height(N, vector<int>(M));
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < M; ++j) {
			cin >> height[i][j];
		}
	}
	vector<Interval> intervals;
	for (int j = 0; j < M; ++j) {
		vector<vector<bool>> visited(N, vector<bool>(M, false));
		queue<pair<int, int>> q;
		q.push({0, j});
		visited[0][j] = true;
		
		int left = M, right = -1;
		
		while (!q.empty()) {
			auto current = q.front();
			q.pop();
			int x = current.first;
			int y = current.second;
			if (x == N - 1) {
				left = min(left, y);
				right = max(right, y);
			}
			
			for (int k = 0; k < 4; ++k) {
				int nx = x + dx[k];
				int ny = y + dy[k];
				
				if (nx >= 0 && nx < N && ny >= 0 && ny < M && !visited[nx][ny] && height[nx][ny] < height[x][y]) {
					visited[nx][ny] = true;
					q.push({nx, ny});
				}
			}
		}
		
		if (right != -1) {
			intervals.push_back({left, right});
		}
	}
	vector<bool> covered(M, false);
	for (const auto& interval : intervals) {
		for (int j = interval.left; j <= interval.right; ++j) {
			covered[j] = true;
		}
	}
	int uncovered = 0;
	for (bool c : covered) {
		if (!c) uncovered++;
	}
	if (uncovered > 0) {
		cout << 0 << endl << uncovered << endl;
		return 0;
	}
	sort(intervals.begin(), intervals.end());
	int count = 0;
	int current_pos = 0;
	int max_right = -1;
	int i = 0;
	while (current_pos < M) {
		max_right = -1;
		while (i < intervals.size() && intervals[i].left <= current_pos) {
			if (intervals[i].right > max_right) {
				max_right = intervals[i].right;
			}
			i++;
		}
		count++;
		current_pos = max_right + 1;
	}
	cout << 1 << endl << count << endl;
	return 0;
}