首页 技术 正文
技术 2022年11月19日
0 收藏 333 点赞 4,734 浏览 3728 个字

F. Polycarp and Hay

题目连接:

http://www.codeforces.com/contest/659/problem/F

Description

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 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.

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.

Sample Input

2 3 35

10 4 9

9 9 7

Sample Output

YES

7 0 7

7 7 7

Hint

题意

给你一个n*m的矩阵,然后给你一个k

这个矩阵里面的数,只能减小,不能增加。

然后你要是的矩阵最后只剩下一个连通块,且连通块里面有一个位置的数没有改变。

连通块的权值和恰好等于k

让你输出一个解。

题解:

把所有数,从大到小排序,然后用并查集去维护

只要当前这个连通块的大小大于等于k/a[i][j]就好了

然后输出的时候用bfs去输出,去维护这个连通块的大小

然后就完了……

代码

#include<bits/stdc++.h>
using namespace std;const int maxn = 1e6+7;
int a[1003][1003],p[maxn],num[maxn],n,m,vis[1003][1003];
long long k;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int fi(int x)
{
return p[x]==x?x:p[x]=fi(p[x]);
}
void uni(int x,int y)
{
int p1=fi(x),p2=fi(y);
if(p1==p2)return;
p[p1]=p2;
num[p2]+=num[p1];
num[p1]=0;
}
struct node
{
int x,y,z;
node(int x1,int y1,int z1){x=x1,y=y1,z=z1;}
};
bool cmp(node a,node b)
{
return a.x>b.x;
}
vector<node>Q;
void solve(int x,int y,int z,int v)
{
queue<node>Q;
Q.push(node(x,y,0));
vis[x][y]=1;z--;
while(!Q.empty())
{
node now = Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
int xx=now.x+dx[i];
int yy=now.y+dy[i];
if(z==0)continue;
if(xx<=0||xx>n)continue;
if(yy<=0||yy>m)continue;
if(vis[xx][yy])continue;
if(a[xx][yy]<v)continue;
vis[xx][yy]=1,z--;
Q.push(node(xx,yy,0));
}
}
for(int i=1;i<=n;i++,cout<<endl)
for(int j=1;j<=m;j++)
if(vis[i][j])printf("%d ",v);
else printf("0 ");
}
int main()
{
scanf("%d%d%lld",&n,&m,&k);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]),Q.push_back(node(a[i][j],i,j));
for(int i=0;i<maxn;i++)p[i]=i,num[i]=1;
sort(Q.begin(),Q.end(),cmp);
for(int i=0;i<Q.size();i++)
{
int v = Q[i].x;if(v==0)break;
int x = Q[i].y;
int y = Q[i].z;
for(int j=0;j<4;j++)
{
int xx = x+dx[j];
int yy = y+dy[j];
if(a[xx][yy]>=v)uni((xx-1)*m+yy,(x-1)*m+y);
}
long long Num = k/v;
if(k%v)continue;
int fa=fi((x-1)*m+y);
if(Num<=num[fa])
{
printf("YES\n");
solve(x,y,Num,v);
return 0;
}
}
printf("NO\n");
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285