题目链接:

题目

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;
}

Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集的更多相关文章

  1. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  2. codeforces 659F F. Polycarp and Hay(并查集+bfs)

    题目链接: F. Polycarp and Hay time limit per test 4 seconds memory limit per test 512 megabytes input st ...

  3. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  4. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  5. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  6. Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  8. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  9. Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

随机推荐

  1. android代码设置、打开WLAN wifi热点及热点的连接

    其实创建热点很简单,先获取到wifi的服务,再配置热点名称.密码等等,然后再通过反射打开它就OK了. 下面我们看看创建热点的代码实现: 这一段是开启WLAN热点,并可以指定好它的热点名和密码 支行后, ...

  2. Objective-C中一个方法如何传递多个参数的理解

    原来如此 Objective-C语法中多参数传递方法经常是初学者最容易犯困的地方.我自己也是刚刚悟出来与大家分享. 分析 由于我们已有的语言经验告诉我们定义方法都是: 一个类型匹配一个参数(动态语言甚 ...

  3. 解决TextView在显示文字的时候,一行还没显示满就跳到下一行

    转载请注明:转自 http://blog.csdn.NET/u011176685/article/details/48295185 一.问题描述: Android的TextView在显示文字的时候,如 ...

  4. Amoeba For MySQL入门:实现数据库水平切分

    当系统数据量发展到一定程度后,往往需要进行数据库的垂直切分和水平切分,以实现负载均衡和性能提升,而数据切分后随之会带来多数据源整合等等问题.如果仅仅从应用程序的角度去解决这类问题,无疑会加重应用程度的 ...

  5. (转)实战Memcached缓存系统(4)Memcached的CAS协议

    1. 什么是CAS协议 很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS ...

  6. [ZZ+CH] Html5 canvas+js 时钟

    总之新Blog入驻以后,又开始老习惯,到处折腾自定义的空间,放些东西. 想起以前大一的时候做过一个Javascript的时间显示器,现在想做一个时钟,当然现在老奸巨猾,会先去看一看有前辈写过没. 前辈 ...

  7. 关于apache Alias斜杠/的实验

    1.Alias /icons/ "D:/wamp/bin/apache/Apache2.2.17/icons/" 访问http://localhost/icons/正常,访问htt ...

  8. TCP/IP:链路层

    链路层主要目的: 1.        为IP模块发送和接收IP数据报. 2.        为ARP模块发送ARP请求和接收ARP应答. 3.        为RARP发送RARP请求和接收RARP应 ...

  9. [转]SET NOCOUNT ON

    ref: http://www.cnblogs.com/jayleke/archive/2010/07/10/1774758.html 在存储过程,触发器中,经常用到SET NOCOUNT ON: 作 ...

  10. 【转】分享10VPN

    以下介绍的vpn,都是有免费流量赠送的免费vpn,完全不免费的不在之列. 免费vpn因为用的人比较多,所以高峰时段可能会有点慢,但是人少时,还是比较顺畅的.对于偶尔浏览外网,看看新闻的同学来说,免费v ...