并查集,$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. 3 MySQL SQL基础

    目录 1. SQL概述2. 数据库操作3. 表操作4. 记录操作 1. SQL概述 SQL,结构化查询语言(Structured Query Language),一种数据库查询和程序设计语言,用于存取 ...

  2. 【ios开发】Block编程

    1 什么是block iOS SDK 4.0开始,Apple引入了block这一特性.字面上说,block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传 ...

  3. asp.net打印网页后自动关闭网页【无需插件】

    项目遇需要网页加载自动打印网页后需要自动关闭该网页,但是百度了好久发现都是需要插件什么的 于是就自己摸索摸索,用js弄了个定时器,意外的发现,当打印设置窗口弹出后,定时器就暂停了 不管你点击取消或者打 ...

  4. C/C++基础知识总结——继承与派生

    1. 类的继承与派生 1.1 派生类的定义 (1) 定义规范 class 派生类名: 继承方式 基类1名, 继承方式 基类2名... { ...派生类成员声明; }; (2) 从以上形式上看可以多继承 ...

  5. PHP实现一个简单url路由功能

    如果一个页面的内容呈现,需要根据url上传递的参数来进行渲染.很多时候可能是这样子写:xxx.com/xx?c=x&m=x& t=..,而我们看到的url往往是这样子的(以新浪微游戏的 ...

  6. WampServer Mysql配置

    WAMP:Windows下的Apache+Mysql+Perl/PHP/Python,一组常用来搭建动态网站或者服务器的开源软件.可点击此处下载WampServer,然后,按照提示安装WAMP.需要说 ...

  7. #define命令的一些高级用法

    =========================================================== define中的三个特殊符号:#,##,#@ ================= ...

  8. Linux内核网络协议栈优化总纲

    本文原创为freas_1990  转载请标明出处:http://blog.csdn.net/freas_1990/article/details/9474121 Jack:淫龙,Linux内核协议栈如 ...

  9. TCP连接的建立与终止

    TCP/IP详解学习笔记(13)-- TCP连接的建立与终止 1.TCP连接的建立            设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请 ...

  10. 创建简单的响应式HTML5模版

    创建简单的响应式HTML5模版 HTML5目前发展势头良好,已经逐渐得到大部分浏览器不同程度的支持.许多web开发者也已经学习到了不少关于HTML 5的基础知识并开始试图使用HTML 5制作网页.与此 ...