Codeforces Round #346 (Div. 2) F bfs+并查集



链接:戳这里


F. Polycarp and Hay
time limit per test4 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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.

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的矩阵(1<=n,m<=1e3),以及一个k(1<=k<=1e18)

要求在矩阵对一些数进行减少(可以变成0)使得矩阵满足

1:在连通块内的所以值都是一样,且连通块里面只好保证一个a[i][j]的值不变

2:其余不是连通块内的点的权值全为0

3:连通块内所有的值加起来正好等于k

4:连通块内的值尽可能的大

如果满足输出YES 并输出更改成连通块之后的矩阵 否则输出NO


思路:

首先我们要保证连通块内的值尽可能的大而且保证一个a[i][j]的值不变,其实这两个条件是题目变简单了

我们只需要把所有的权值排个序,然后从大到小判断当前这个值作为那个不变的a[i][j]是否可以满足条件

对于当前的s[i]=a[i][j]  

因为题目要求矩阵的权值只能减少,所以用并查集处理上下左右四个方向的比它大的值并联通起来

接下来  因为要求k正好整除这个权值  所以算出连通块内需要Num个a[i][j]   

然后去判断当前(i,j)这个位置所在的连通块内是否有>=Num的个数

有的话肯定是YES啊 然后用bfs来填这Num个数

填的过程就很水了  队列再加上标记很快就处理出来了  

我居然看二维数组的时候一直开成[1001][1001]wa了4发    我的天      记得开大一点啊


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
ll K;
ll a[1010][1010],num[MAX],fa[MAX];
int vis[1010][1010];
struct node{
    int x,y;
    ll v;
    node(int x=0,int y=0,ll v=0):x(x),y(y),v(v){}
}s[MAX];
bool cmp(node a,node b){
    return a.v>b.v;
}
int xx[4]={-1,1,0,0};
int yy[4]={0,0,-1,1};
int find(int x){
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
void Union(int x,int y){
    int X=find(x);
    int Y=find(y);
    if(X!=Y){
        fa[X]=Y;
        num[Y]+=num[X];
        num[X]=0;
    }
}
bool pd(node t){
    if(t.x<1 || t.x>n || t.y<1 || t.y>m || vis[t.x][t.y])
        return false;
    return true;
}
void Solve(int x,int y,ll V,ll Num){
    queue<node> qu;
    mst(vis,0);
    vis[x][y]=1;
    Num--;
    qu.push(node(x,y,V));
    while(!qu.empty()){
        node now=qu.front(),next;
        qu.pop();
        for(int i=0;i<4;i++){
            next.x=now.x+xx[i];
            next.y=now.y+yy[i];
            if(Num==0 || a[next.x][next.y]<V) continue;
            if(pd(next)){
                Num--;
                vis[next.x][next.y]=1;
                qu.push(node(next));
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]) printf("%I64d ",V);
            else printf("0 ");
        }
        cout<<endl;
    }
}
int main(){
    int cnt=0;
    scanf("%d%d%I64d",&n,&m,&K);
    for(int i=0;i<=MAX;i++) fa[i]=i,num[i]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%I64d",&a[i][j]);
            s[++cnt]=node(i,j,a[i][j]);
        }
    }
    sort(s+1,s+cnt+1,cmp);
    for(int i=1;i<=cnt;i++){
        if(s[i].v==0) break;
        for(int j=0;j<4;j++){
            int X=s[i].x+xx[j];
            int Y=s[i].y+yy[j];
            if(a[X][Y]>=s[i].v) Union((s[i].x-1)*m+s[i].y,(X-1)*m+Y);
        }
        ll Num=K/s[i].v;
        if(K%s[i].v) continue;
        int u=find((s[i].x-1)*m+s[i].y);
        if(Num<=num[u]){
            printf("YES\n");
            Solve(s[i].x,s[i].y,s[i].v,Num);
            return 0;
        }
    }
    printf("NO\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值