Codeforces Round #346 (Div. 2) F. Polycarp and Hay(并查集)

来源:互联网 发布:网络剧无心法师 编辑:程序博客网 时间:2024/05/22 02:17
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 3510 4 99 9 7
output
YES7 0 7 7 7 7 
input
4 4 505 9 1 15 1 1 55 1 5 55 5 7 1
output
YES5 5 0 0 5 0 0 5 5 0 5 5 5 5 5 0 
input
2 4 121 1 3 11 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 (1 ≤ n,m ≤ 1000) and k (1 ≤ k ≤ 10^18),n,m表示一个 n*m的矩阵,矩阵中的每个元素小于等于10^9,问能不能构成另外一个矩阵。

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

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

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

让你输出一个解。

 

思路:对所有的数从大到小排序,然后把相邻的边也用结构体存起来,边的长度为相邻两点的较小值,从大到小排序,然后用并查集维护,如果如果当前的边的长度大于等于当前的长度,则把两个点并起来,维护这个集合中的结点的数目。只要当前这个连通块的大小大于等于k/a[i][j]就好了

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

坑点:有可能最后的矩阵只剩下一个数。

#include<bits/stdc++.h>using namespace std;typedef pair<int,int> PI;typedef __int64 ll;const int maxn=4001000;int f[maxn/4],sz[maxn/4];int find(int x){    int k, j, r;    r = x;    while(r != f[r])        r = f[r];    k = x;    while(k != r){        j = f[k];        f[k] = r;        k = j;    }    return r;}struct Edge{    int from,to,num;}e[maxn];int a[1010][1010],num[maxn];int vis[1010][1010];int dir[4][2]={1,0,0,1,-1,0,0,-1};bool cmp(int x,int y){    return x>y;}bool cmp1(Edge x,Edge y){    return x.num>y.num;}struct node{    int x,y;};map<int,int>mp;int T,n,m;ll ans1;bool bfs(int ans,int count,int x,int y){    queue<node>Q;    Q.push((node){x,y});    vis[x][y]=T;    int cnt=1;    while(!Q.empty()){        node u=Q.front();        Q.pop();        if(cnt==ans)            return true;        for(int i=0;i<4;i++){            int x=u.x+dir[i][0],y=u.y+dir[i][1];            if(x<=0||x>n||y<=0||y>m||a[x][y]<count||vis[x][y]!=0)                continue;            vis[x][y]=T;            Q.push((node){x,y});            cnt++;            if(cnt==ans)                return true;        }    }    return false;}void solve(int cnt,int count){    int flag=0;    T=0;    for(int i=1;i<=n;i++){        for(int j=1;j<=m;j++){            T++;            if(a[i][j]==count&&vis[i][j]==0)                if(bfs(cnt,count,i,j)){                    flag=1;                    break;                }        }        if(flag==1)            break;    }}int main(){    int tot=0;    ll k;    scanf("%d%d%I64d",&n,&m,&k);    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++){            scanf("%d",&a[i][j]);            num[++tot]=a[i][j];        }    sort(num+1,num+tot+1,cmp);    tot=unique(num+1,num+tot+1)-num-1;    int cnt=0;    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)            for(int u=0;u<2;u++){                int x=i+dir[u][0],y=j+dir[u][1];                if(x<=0||x>n||y<=0||y>m)                    continue;                e[++cnt].from=(i-1)*m+j,e[cnt].to=(x-1)*m+y;                e[cnt].num=min(a[i][j],a[x][y]);            }    for(int i=1;i<=n*m;i++)        f[i]=i,sz[i]=1;    sort(e+1,e+cnt+1,cmp1);    for(int i=1,j=1;i<=tot;i++){        int maxv=1; //最少有一个        while(j<=cnt&&e[j].num==num[i]){            int x=find(e[j].from),y=find(e[j].to);            if(x!=y){                f[x]=y;                sz[y]+=sz[x];            }            j++;            maxv=max(maxv,sz[y]);        }        mp[num[i]]=maxv;    }    ll flag=0,ans;    for(int i=1;i<=tot;i++){        if(k%num[i]==0&&mp[num[i]]>=k/num[i]){            flag=1;            ans=num[i];            solve(k/num[i],num[i]);            break;        }    }    if(flag==0)        printf("NO\n");    else{        printf("YES\n");        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++){                if(j==m)                    printf("%d\n",vis[i][j]==T ? ans:0);                else                    printf("%d ",vis[i][j]==T ? ans:0);            }    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 花钱花多了后悔怎么办 想去当兵有纹身怎么办 爸家里人欺负我妈怎么办 部队退伍登记表填错怎么办 新手程序员第一次做项目怎么办 十六岁月经量少怎么办 大姨妈第一天痛怎么办 月经第一天很疼怎么办 神器挑战任务放弃了怎么办 内蒙古森林武警改革新兵怎么办 手挨打了有点肿怎么办 征兵过程中身份证号重复怎么办 去当兵身上有纹身怎么办 武警警卫部队改革新兵怎么办 肾结石3mm很痛怎么办 剖腹产第6天肾结石痛怎么办 左肾5mm结石怎么办 肾结石手术后反复发烧怎么办 百世快递会被退回怎么办 身份证被列入黑名单了怎么办 顺丰寄方客户要求退回快递费怎么办 蛋蛋被皮筋弹肿了怎么办 人肌肉里的绦虫卵怎么办 鞋子前面踢坏了怎么办 猫割完蛋蛋流东西怎么办 北京怡瑞被骗后怎么办 玩滑板睾丸碎了怎么办 雄鸽不会踩蛋怎么办 玩滑板蛋碎了怎么办 精子在精囊满了怎么办 孩子的睾丸一大一小怎么办 肾阳虚早射该怎么办 有奶水吸不出来怎么办 苹果mac商店里没有软件怎么办 战地1安装包损坏怎么办 战地4db显示数据异常怎么办 战地1亚服没人怎么办 战地一加载太慢怎么办 饥荒手机版怪物跟人就打狗包怎么办 电脑文件损坏开不了机怎么办 黑魂1武器损坏怎么办