题目链接:

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.

Input

The first line of the input contains three integers nm (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.

Output

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.

Examples
input
2 3 35
10 4 9
9 9 7
output
YES
7 0 7
7 7 7
input
4 4 50
5 9 1 1
5 1 1 5
5 1 5 5
5 5 7 1
output
YES
5 5 0 0
5 0 0 5
5 0 5 5
5 5 5 0
input
2 4 12
1 1 3 1
1 6 2 4
output
NO
Note

In the first sample non-zero values make up a connected area, their values do not exceed the initial heights of hay stacks. All the non-zero values equal 7, and their number is 5, so the total volume of the remaining hay equals the required value k = 7·5 = 35. At that the stack that is on the second line and third row remained unaltered.

题意

给这么一个n*m矩阵,里面的值只能减小不能增大,要求是否可以得到连在一块的且和为k的连通块,而且这里面至少有一个的值没变;

思路

先按从大到小的顺序排序,然后再把挨着的用并查集连在一块,同时更新这个集里面的元素个数,等操作到这个元素的值*这个元素所属集的元素个数>=k&&k能整除元素的值的时候就是能输出答案的时候了;再用bfs找到那些要求的地方输出就行,注意使用并查集要路径压缩,否则会tle;

AC代码:

/*
2014300227 659F - 62 GNU C++11 Accepted 1044 ms 33596 KB
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+;
int p[N],a[][],num[N],n,m,ans[][],vis[][];
int dir[][]={,-,,,,,-,};
ll k;
struct node
{
int hi,x,y;
};
node po[N];
int cmp(node u,node v)
{
return u.hi>v.hi;
}
int findset(int x)
{
if(x==p[x])return x;
return p[x]=findset(p[x]);
}
void same(int x,int y)
{
int fx=findset(x),fy=findset(y);
if(fx!=fy)
{
p[fx]=fy;
num[fy]+=num[fx];
num[fx]=;
}
}
int print(node r)
{
memset(vis,,sizeof(vis));
node temp;
int nu=;
queue<node>qu;
qu.push(r);
vis[r.x][r.y]=;
while(!qu.empty())
{
node to=qu.front();
qu.pop();
for(int i=;i<;i++)
{
int px=to.x+dir[i][],py=to.y+dir[i][];
if(px<||px>n||py<||py>m)continue;
if(a[px][py]>=r.hi&&nu<k/r.hi&&vis[px][py]==)
{
temp.hi=a[px][py];
temp.x=px;
temp.y=py;
qu.push(temp);
vis[px][py]=;
nu++;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(vis[i][j])
{
printf("%d ",r.hi);
}
else printf("0 ");
}
printf("\n");
} }
int main()
{
int cnt=;
cin>>n>>m>>k;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
po[cnt].hi=a[i][j];
po[cnt].x=i;
po[cnt].y=j;
cnt++;
}
}
for(int i=;i<N;i++)
{
p[i]=i;
num[i]=;
}
sort(po+,po+cnt+,cmp);
for(int i=;i<cnt;i++)
{ for(int j=;j<;j++)
{
int fx=po[i].x+dir[j][],fy=po[i].y+dir[j][]; if(fx<=||fx>n||fy<=||fy>m)continue;
if(a[fx][fy]>=po[i].hi)
{
same((fx-)*m+fy,(po[i].x-)*m+po[i].y);
}
}
if(k%po[i].hi==){
int fa=findset((po[i].x-)*m+po[i].y);
ll s=(ll)num[fa]*(ll)po[i].hi;
if(s>=k)
{
cout<<"YES"<<"\n";
print(po[i]);
return ;
}
}
}
printf("NO\n"); return ;
}

codeforces 659F F. Polycarp and Hay(并查集+bfs)的更多相关文章

  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 Round #346 (Div. 2) F. Polycarp and Hay 并查集

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

  3. Codeforces 659F Polycarp and Hay 并查集

    链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...

  4. [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  5. 【17.69%】【codeforces 659F】Polycarp and Hay

    time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  6. codeforces #541 F Asya And Kittens(并查集+输出路径)

    F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...

  7. 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E

    http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...

  8. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  9. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

随机推荐

  1. 使用matlab进行mex编译时的路径问题mexopts

            matlab和vs 进行混合编程时总须要使用matlab编译mexFunction.cpp文件. 这些文件免不了使用include下的*.h和lib下的*.lib文件.举例说明.这次我 ...

  2. ARM内核和架构

    转:深入浅谈,CPU设计原理          CPU的内部架构和工作原理 推荐一本书:编码的奥秘 一.ARM内核和架构 ARM产品越来越丰富,命名也越来越多.很多朋友提问: ARM内核和架构都是什么 ...

  3. python3 configparser对配置文件读写

    import configparser #read data from conf filecf=configparser.ConfigParser()cf.read("biosver.cfg ...

  4. 查看mysql 的存储过程定义

    查询数据库中的存储过程 方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE' 方法 ...

  5. jQuery的Pagenation分页插件。

    插件简介 此jQuery插件为Ajax分页插件,一次性加载,故分页切换时无刷新与延迟,如果数据量较大不建议用此方法,因为加载会比较慢. 原插件CSS不太合理,使用浮动,故无法方便实现左右方向的定位,且 ...

  6. 度度熊有一张网格纸,但是纸上有一些点过的点,每个点都在网格点上,若把网格看成一个坐标轴平行于网格线的坐标系的话,每个点可以用一对整数x,y来表示。度度熊必须沿着网格线画一个正方形,使所有点在正方形的内部或者边界。然后把这个正方形剪下来。问剪掉正方形的最小面积是多少。

    // ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  7. 【Python基础】之异常

    一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...

  8. 【WPF学习笔记】之如何传递第一个登录界面的值到下一个页面显示:动画系列之(三)

    ... ... 承接系列(二) 在之前的登录后台已设置发送到主界面: 在主界面接收传递的值: using System; using System.Collections.Generic; using ...

  9. sqlite3常用操作命令 和mysql的区别及优缺点

    SQLite 的数据库权限只依赖于文件系统,没有用户帐户的概念. sqlite3 testdb.db .databases 命令查看数据库列表 create table tbl(name char(1 ...

  10. vue 流程设计器

    github地址:https://github.com/280780363/gucflow.designer demo地址:https://280780363.github.io/gucflow.de ...