题目链接:

题目

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. Javasript中Date日期常用用法(正则、比较)

    Date 对象用于处理日期和时间.创建 Date 对象的语法:  代码如下 复制代码 var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值.参数形式有以下5种 ...

  2. JavaScript 函数的执行过程

    每一个JavaScript函数都是Function对象的一个实例, 它有一个仅供JavaScript引擎存取的内部属性[[Scope]]. 这个[[Scope]]存储着一个作用域的集合, 这个集合就叫 ...

  3. [C#]线程处理

    线程处理用于使程序能够执行并发处理,同时执行多个操作.C#中有三种线程的使用方法,BackgroundWorker组件.线程池.自己创建使用线程,接下来分别介绍如何使用. 1.使用Background ...

  4. 图像热点&图像映射

    图像映射 图像映射也称为图像热点. 作用: 让同一张图片上的不同区域,可以实现多个不同的超链接功能. 图示: <map>图像映射三步走: 图像映射的实现需要三方面配合完成: 1.图像映射容 ...

  5. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  6. OOP—还原被遮掩的继承名称

    1.public继承——using 声明式 class Base { private: int x; public: ; virtual void mf1(int) ; virtual void mf ...

  7. Powerful Sleep(神奇的睡眠-睡眠生物钟的秘密:如何睡得更少却睡得更好)阅读笔记

    睡眠机制 我们活着的时候,大脑会产生脑电波.脑电图仪器通过贴在人头上的一些电极读出脑电波的活动,然后把活动用图表显示出来. 睡眠过程可以分为5个过程,划分依据与大脑发出的脑电波类型. 当人清醒时,大脑 ...

  8. Linux的网卡由eth0变成了eth1,如何修复 (转载)

    Linux的网卡由eth0变成了eth1,如何修复   使用wmware安装了linux,安装成功后,使用的网卡是eth0,没有eth1.但是用过一段时间后,不知道为什么eth0无法使用,系统却自动生 ...

  9. [DevExpress]GridControl 同步列头checkbox与列中checkbox状态

    关键代码: /// <summary> /// 同步列头checkbox与列中checkbox状态 /// </summary> /// <param name=&quo ...

  10. Jxl操作excel的demo

    网上很多例子,都是用Jxl读或者写excel,本文实现的功能就是将数据源in.xls的第几行第几列数据写入到out.xls的第几行第几列,不覆盖out.xls其他原有的数据. 需要导入的包:jxl.j ...