codeforces 659F F. Polycarp and Hay(并查集+bfs)

题目链接:

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+4;
int p[N],a[1003][1003],num[N],n,m,ans[1003][1003],vis[1003][1003];
int dir[4][2]={0,-1,0,1,1,0,-1,0};
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]=0;
    }
}
int print(node r)
{
    memset(vis,0,sizeof(vis));
    node temp;
    int nu=1;
    queue<node>qu;
    qu.push(r);
    vis[r.x][r.y]=1;
    while(!qu.empty())
    {
        node to=qu.front();
        qu.pop();
        for(int i=0;i<4;i++)
        {
            int px=to.x+dir[i][0],py=to.y+dir[i][1];
            if(px<1||px>n||py<1||py>m)continue;
            if(a[px][py]>=r.hi&&nu<k/r.hi&&vis[px][py]==0)
            {
                temp.hi=a[px][py];
                temp.x=px;
                temp.y=py;
                qu.push(temp);
                vis[px][py]=1;
                nu++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(vis[i][j])
            {
                printf("%d ",r.hi);
            }
            else printf("0 ");
        }
        printf("\n");
    }

}
int main()
{
    int cnt=1;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;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=1;i<N;i++)
    {
        p[i]=i;
        num[i]=1;
    }
    sort(po+1,po+cnt+1,cmp);
    for(int i=1;i<cnt;i++)
    {

        for(int j=0;j<4;j++)
        {
            int fx=po[i].x+dir[j][0],fy=po[i].y+dir[j][1];

            if(fx<=0||fx>n||fy<=0||fy>m)continue;
            if(a[fx][fy]>=po[i].hi)
            {
                same((fx-1)*m+fy,(po[i].x-1)*m+po[i].y);
            }
        }
            if(k%po[i].hi==0){
            int fa=findset((po[i].x-1)*m+po[i].y);
            ll s=(ll)num[fa]*(ll)po[i].hi;
            if(s>=k)
            {
                cout<<"YES"<<"\n";
                print(po[i]);
                return 0;
            }
            }
    }
    printf("NO\n");

    return 0;
}

 

posted @ 2016-03-31 23:06  LittlePointer  阅读(411)  评论(0编辑  收藏  举报