题目链接:

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. 3、C++新的关键字

        C++ 添加了一些全新的关键字. 1.new     new 来进行动态内存的分配,而delect 则是进行内存的释放, 申请的方式: 变量申请: int *p = new int; // 申 ...

  2. 2、C++ 的升级

    1.内联函数     define 可以定义宏代码片段,但是,C++ 推荐使用内联函数替代宏代码片段. inline int f(int a, int b) { }     只需要在 函数定义(实现) ...

  3. Redis(十):使用RedisTemplate执行Redis脚本

    对于Redis脚本使用过的同学都知道,这个主要是为了防止竞态条件而用的.因为脚本是顺序执行的.(不用担心效率问题)比如我在工作用,用来设置考试最高分. 如果还没有用过的话,先去看Redis脚本的介绍, ...

  4. 集群通信组件Tribes之怎样维护集群成员信息

    一个集群包括若干成员,要对这些成员进行管理就必需要有一张包括全部成员的列表.当要对某个节点做操作时通过这个列表能够准确找到该节点的地址进而对该节点发送操作消息.怎样维护这张包括全部成员的列表是本节要讨 ...

  5. Shell脚本笔记 1

    函数别名 设置别名 alias name="command" alias ll="ls -laS" 取消别名 unalias name 求取数学表达式 valu ...

  6. HDFS源码分析EditLog之获取编辑日志输入流

    在<HDFS源码分析之EditLogTailer>一文中,我们详细了解了编辑日志跟踪器EditLogTailer的实现,介绍了其内部编辑日志追踪线程EditLogTailerThread的 ...

  7. java array

    1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...

  8. 为什么Java中的字符串是不可变的?

    原文链接:https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/ java字符串是不可变的.不可变类只是一个不能修改 ...

  9. centos7 安装jdk9 总结

    升级jdk, 从jdk8 升级到jdk9 1:卸载jdk8: 1〉 [root@localhost conf.d]# rpm -qa|grep java javapackages-tools-3.4. ...

  10. Java中的迭代迭代器Iterator与枚举器Enumeration

    Iterator 和 Enumeration区别 Iterator 和 Eumberation都是Collection集合的遍历接口,我们先看下他们的源码接口 package java.util; p ...