并查集,$dfs$。

从大的数字往里加,每加一个数字合并一下连通块,判断连通块内数字个数是否够,以及k能不能被当前加入的数字整除。然后$dfs$一下构造答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
int n,m,sz,c; LL k;
struct X { int r,c; LL v; }s[maxn*maxn];
int fa[maxn*maxn],a[maxn][maxn],cnt[maxn*maxn]; LL ans[maxn][maxn];
bool f[maxn][maxn],q;
int dir[][]={{-,},{,},{,-},{,}}; int Find(int x)
{
if(x!=fa[x]) fa[x]=Find(fa[x]);
return fa[x];
} bool cmp(X a,X b) { return a.v<b.v; } void work(int a,int b)
{
if(b<||b>=n*m) return; int fx=Find(a),fy=Find(b); if(f[b/m][b%m]==) return; if(fx==fy) return;
fa[fy]=fx;
cnt[fx]=cnt[fx]+cnt[fy]; cnt[fy]=;
} bool check(int a,int b)
{
if(a>=&&a<n&&b>=&&b<m) return ;
return ;
} void dfs(int x,int y,LL v,int f)
{
c++; ans[x][y]=v; if(c>=f) return;
for(int i=;i<;i++)
{
int tx=x+dir[i][],ty=y+dir[i][];
if(!check(tx,ty)) continue;
if(a[tx][ty]<v) continue;
if(ans[tx][ty]!=) continue;
dfs(tx,ty,v,f); if(c>=f) return;
}
} int main()
{
scanf("%d%d%lld",&n,&m,&k);
for(int i=;i<n*m;i++) fa[i]=i,cnt[i]=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%lld",&a[i][j]);
s[sz].r=i; s[sz].c=j; s[sz].v=a[i][j]; sz++;
}
}
sort(s,s+sz,cmp); for(int j=sz-; j>=; j--)
{
int id1=s[j].r*m+s[j].c,id2;
if(check(s[j].r-,s[j].c))
{
id2=(s[j].r-)*m+s[j].c;
work(id1,id2);
}
if(check(s[j].r+,s[j].c))
{
id2=(s[j].r+)*m+s[j].c;
work(id1,id2);
}
if(check(s[j].r,s[j].c-))
{
id2=s[j].r*m+s[j].c-;
work(id1,id2);
}
if(check(s[j].r,s[j].c+))
{
id2=s[j].r*m+s[j].c+;
work(id1,id2);
}
f[s[j].r][s[j].c]=; int d=Find(id1);
if(k%s[j].v==&&(LL)cnt[d]>=k/s[j].v)
{
dfs(s[j].r,s[j].c,s[j].v,(int)(k/s[j].v));
q=; break;
}
if(q) break;
} if(!q) { printf("NO\n"); return ;}
printf("YES\n");
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
printf("%lld",ans[i][j]);
printf(" ");
}
printf("\n");
}
return ;
}

CodeForces 659F Polycarp and Hay的更多相关文章

  1. Codeforces 659F Polycarp and Hay 并查集

    链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...

  2. Codeforces 659F Polycarp and Hay【BFS】

    有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发...好吧,其实也就这一次... 小白说的对,还是代码能力不足... 非常不足... 题目链接: http://codefo ...

  3. codeforces 659F . Polycarp and Hay 搜索

    题目链接 遍历每个点, 如果这个点的值能被k整除并且k/a[i][j]后小于等于n*m, 那么就对这个点进行搜索. 将这个点加入队列, 将周围的所有大于等于这个点的值的点也加入队列. 不断重复, 直到 ...

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

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

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

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

  7. 【17.69%】【codeforces 659F】Polycarp and Hay

    time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  8. Codeforces 723C. Polycarp at the Radio 模拟

    C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  9. codeforces 727F. Polycarp's problems

    题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...

随机推荐

  1. [转]Libev教程

    libev Table of Contents 1 libev 1.1 About The Code 1.2 EventLoop 1.3 Watcher 1.4 How it works 1.4.1 ...

  2. 使用VS2010命令提示窗口操作程序集强命名

    说明:文中示例均以将文件置于D盘根目录(D:\)下为例. 一.查看程序集是否具有强命名 sn -T d:\LicBase.dll 若有则会显示PublicKeyToken值,反之不会. 二.给无强命名 ...

  3. jQuery插件综合应用1

    jQuery插件综合应用(一)注册   一.介绍 注册和登录是每个稍微有点规模的网站就应该有的功能.登陆功能与注册功能类似,也比注册功能要简单些.所以本文就以注册来说明jQuery插件的应用. jQu ...

  4. 企业架构研究总结(32)——TOGAF架构内容框架之架构交付物

    3. 架构交付物(Architecture Deliverables) 架构交付物是在整个架构开发方法循环过程中所产生或被使用的契约性且正规化的企业架构内容,因而其与企业架构开发方法有着紧密的联系.本 ...

  5. Oracle误删恢复

    query deleted datarows: select * from 表名 as of timestamp to_timestamp('删除时间点','yyyy-mm-dd hh24:mi:ss ...

  6. Windows7和Archlinux双系统硬盘安装笔记

    俗话说,好记性不如烂笔头,这些东西也都是我Google来的,做个笔记以后自己安装也方便些. 因为官方wiki的Beginners' Guide讲的非常好,大部分步骤按照wiki一步一步来就好了,这里只 ...

  7. web form中自定义HttpHandler仿mvc

    web form中自定义HttpHandler仿mvc 前言 在mvc大行其道的今天,仍然有不少公司的项目还是使用web form来实现的(其实mvc也是基于web form的),如果要在项目中引入m ...

  8. T-SQL查询语句(二):嵌套查询

    一个select...From...Where查询语句块可以嵌套在另一个select...From...Where查询块的Where子句中,称为嵌套查询.外层查询称为父查询,主查询.内层查询称为子查询 ...

  9. linux前四天学习笔记

    以下是在linux培训机构所学的内容,感觉比较乱 MySQL学习笔记MySQL的安装 linux中的超级管理员rootaixocm vnc的退出: F8 MySQL的特点.优点:关系型开源.免费c++ ...

  10. PHP之语言基础01 By ACReaper

    1.PHP中的变量是不需要声明类型的,由$标识变量,变量的命名规则也是字母或者下划线开头,接着任意字符或者下划线. $PI = 3.14; $radius = 5; $cir = $PI * 2 * ...