题目链接:

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. css:清除浮动 overflow

    是因为overflow除了(visible)会重新给他里面的元素建立块级格式化(block formatting context)floats, position absolute, inline-b ...

  2. ThinkPHP中的模型命名

    当我们创建一个UserModel类的时候,其实已经遵循了系统的约定.ThinkPHP要求数据库的表名和模型类的命名遵循一定的规范,首先数据库的表名和字段全部采用小写形式,模型类的命名规则是除去表前缀的 ...

  3. MySQL-[Err] 1055 - Expression #1

    © 版权声明:本文为博主原创文章,转载请注明出处 问题描述:在MySQL数据库下,执行SQL插入语句报错.错误信息如下: 错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL ...

  4. jdk并发工具包之锁

    1.cynchronized扩展:可重锁入ReentrantLock ReentrantLock是通过cas算法实现的 RenntrantLock lock=new ReentrantLock(); ...

  5. jdbcTemplaate queryForObject的两个易混淆的方法

    JdbcTemplate中有两个可能会混淆的queryForObject方法: 1.    Object queryForObject(String sql, Object[] args, Class ...

  6. Maven的安装以及在IDEA中的配置

    Maven的安装 之前的一篇博客中已经写到过了Maven的安装.这里就只给出链接了. http://www.cnblogs.com/tuhooo/p/5905569.html 版本虽然不同,但是安装的 ...

  7. 世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?

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

  8. IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。 HTTP 错误 500.19

    因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运行命令行 %windir%\system32\inetsrv\appcmd unlock conf ...

  9. golang 内存池

    一般来说,内存池都是采用预分配的方式,分为固定大小的和非固定大小块,固定大小的内存效率高,非固定大小灵活.同时,分为单线程和多线程版的,单线程不需要考虑并发问题. 一般内存池的实现思想:分配一块比较大 ...

  10. Javaweb之 servlet 开发具体解释1

    1.1  Tip:Servlet简单介绍 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发 ...