Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
题目链接:
题目
F. Polycarp and Hay
time limit per test: 4 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output
问题描述
The farmer Polycarp has a warehouse with hay, which can be represented as an n × m rectangular table, where n is the number of rows, and m is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the i-th row and the j-th column is equal to an integer ai, j and coincides with the number of cubic meters of hay in the haystack, because all cells have the size of the base 1 × 1. Polycarp has decided to tidy up in the warehouse by removing an arbitrary integer amount of cubic meters of hay from the top of each stack. You can take different amounts of hay from different haystacks. Besides, it is allowed not to touch a stack at all, or, on the contrary, to remove it completely. If a stack is completely removed, the corresponding cell becomes empty and no longer contains the stack.
Polycarp wants the following requirements to hold after the reorganization:
the total amount of hay remaining in the warehouse must be equal to k,
the heights of all stacks (i.e., cells containing a non-zero amount of hay) should be the same,
the height of at least one stack must remain the same as it was,
for the stability of the remaining structure all the stacks should form one connected region.
The two stacks are considered adjacent if they share a side in the table. The area is called connected if from any of the stack in the area you can get to any other stack in this area, moving only to adjacent stacks. In this case two adjacent stacks necessarily belong to the same area.
Help Polycarp complete this challenging task or inform that it is impossible.
输入
The first line of the input contains three integers n, m (1 ≤ n, m ≤ 1000) and k (1 ≤ k ≤ 1018) — the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization.
Then n lines follow, each containing m positive integers ai, j (1 ≤ ai, j ≤ 109), where ai, j is equal to the number of cubic meters of hay making the hay stack on the i-th row and j-th column of the table.
输出
In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next n lines print m numbers — the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a connected area and at least one of these values shouldn't be altered.
If there are multiple answers, print any of them.
样例
input
2 3 35
10 4 9
9 9 7
output
YES
7 0 7
7 7 7
题意
给你一块n*m的草地,每块草地高度为h[i][j]。
现在你要选择一根草的高度,将其他的草削成它那么高,或是直接拔掉。从而使最后剩下的草高度一致,且所它们属于同一个连通分量,且它们的高度和等于k。
如果存在多个满足的情况,任意输出一种
题解
把所有的草按高度从高到矮插到田里去,用并查集维护连通分量的大小,直到满足条件为止。
代码
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 1111;
typedef __int64 LL;
struct Node {
int val, x, y;
Node(int val, int x, int y) :val(val), x(x), y(y) {}
bool operator < (const Node& tmp)const {
return val > tmp.val;
}
};
int mat[maxn][maxn];
int n, m;
LL k,ans;
int f[maxn][maxn],siz[maxn][maxn];
const int base = 10000;
int find(int v) {
int x = v / base, y = v%base;
return f[x][y] = f[x][y] == v ? v : find(f[x][y]);
}
const int dx[] = { -1,1,0,0 };
const int dy[] = { 0,0,-1,1 };
bool solve(int val, int x, int y) {
mat[x][y] = val; siz[x][y] = 1;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (mat[tx][ty]) {
int anc = find(tx*base + ty);
if(anc!=f[x][y]){
siz[x][y] += siz[anc / base][anc%base];
f[anc / base][anc%base] = f[x][y];
}
}
}
if (k%val==0&&k/val<=siz[x][y]) return true;
return false;
}
int vis[maxn][maxn];
void dfs(int x, int y,int &cnt) {
vis[x][y] = 1; cnt++;
if (cnt >= k / ans) return;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (!vis[tx][ty] && mat[tx][ty]) {
dfs(tx, ty, cnt);
}
if (cnt >= k / ans) return;
}
}
void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = i*base + j;
}
}
memset(mat, 0, sizeof(mat));
memset(siz, 0, sizeof(siz));
memset(vis, 0, sizeof(vis));
}
int main() {
scanf("%d%d%I64d", &n, &m, &k);
init();
vector<Node> arr;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x; scanf("%d", &x);
arr.push_back(Node(x, i, j));
}
}
sort(arr.begin(), arr.end());
int su = 0;
for (int i = 0; i < arr.size(); i++) {
if (solve(arr[i].val, arr[i].x, arr[i].y)) {
ans=arr[i].val;
int cnt = 0;
dfs(arr[i].x, arr[i].y,cnt);
su = 1;
break;
}
}
if (su) {
puts("YES");
for (int i = 1; i <= n; i++) {
for (int j = 1; j < m; j++) {
if (vis[i][j]) printf("%d ", ans);
else printf("0 ");
}
if (vis[i][m]) printf("%d\n", ans);
else printf("0\n");
}
}
else {
puts("NO");
}
return 0;
}