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");
}

Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs的更多相关文章

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

  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. JS的全局函数eval解析JSON字符串

    JavaScript eval() 函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需. ...

  2. 多线程中的超时, 如Socket超时

    ; ,,, ->$port { print "-->$port\r"; #say "\r"; await Promise.anyof( Promis ...

  3. Linux是对用户的密码的复杂度要求设置【转】

    那么Linux是如何实现对用户的密码的复杂度的检查的呢?其实系统对密码的控制是有两部分组成: 1 cracklib 2 /etc/login.defs pam_cracklib.so 才是控制密码复杂 ...

  4. 函数参数 f_arg, *args, **kwargs

    当需要给函数传参时,可以通过运用不同的形参来传递,达到参数不同的使用目的. 简单来说:f_arg就是传递的第一个参数,类似于C++中的普通参数: *args 传递的是一个参数的list: **kwar ...

  5. ie7浏览器兼容问题

    win10 下如何调试Ie 网上有很多ie的测试工具,包括ms自己出的有,但是如果是win10系统,压根不需要这些玩意. win10 浏览器edge虽然是重写过的,但是win10并没有完全抛弃ie,可 ...

  6. Mybatis的初步使用

    MyBatis 是当下最流行的持久层框架,也是ORM框架,本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google ...

  7. linux和windows下TIME_WAIT过多的解决办法

    http://www.51testing.com/html/48/202848-249774.html linux和windows下TIME_WAIT过多的解决办法 http://m.sohu.com ...

  8. 修改TortoiseSVN客户端登陆用户

    TortoiseSVN是一款常用且非常不错的SVN工具,俗称小乌龟.开发的时候,经常用的当然是TortoiseSVN客户端了. 一般情况下,TortoiseSVN服务器提供的IP地址和用户都不会变,而 ...

  9. POJ 1392 Ouroboros Snake(数位欧拉)

    题目链接:http://poj.org/problem?id=1392 题目大意:题意看的我头痛,其实跟HDU2894差不多,但是这题要求输出这条路径上第k个数,而不是输出路径. 解题思路:也跟HDU ...

  10. Codeforces 813B The Golden Age(数学+枚举)

    题目大意:如果一个数t=x^a+y^b(a,b都是大于等于0的整数)那就是一个unlucky数字.给你x,y,l,r(2 ≤ x, y ≤ 10^18, 1 ≤ l ≤ r ≤ 10^18),求出l到 ...