并查集,$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. C# BackgroundWorker组件学习

    C# BackgroundWorker组件学习  C# BackgroundWorker组件学习 一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用Ba ...

  2. 哞哞快的 C# 高斯模糊实现(续)

    昨天刚写了<哞哞快的 C# 高斯模糊实现>,里边提到了用原作者的方法实现对图像快速的高斯模糊处理,说实话,我没看懂,主要是没看懂原理,怎么就“把图片给处理了”,大概是调用了 GDIPlus ...

  3. 添加可运行的js代码

    如何在博客园的文章/随笔中添加可运行的js代码 在博客园浏览大牛们写的文章时,经常会看到在文章中混有一些可运行示例,例如司徒正美的博客中: 带有可运行示例 可以点击“运行代码” 经过一番小小的探索,掌 ...

  4. Page_Load接收随机参数放到字典类中

    Page_Load接收随机参数放到字典类中,可以用作签名.普通的接收url的参数可以用作下面这种模式:  int appid =Convert.ToInt32(param["appid&qu ...

  5. 博客已迁移到lizhug.com

    新的博客地址 http://lizhug.com

  6. ASP.NET Web API的消息处理管道: HttpRoutingDispatcher

    ASP.NET Web API的消息处理管道: HttpRoutingDispatcher 认情况下,作为消息处理管道“龙头”的HttpServer的Dispatcher属性返回一个HttpRouti ...

  7. Oracle体系结构及备份(十七)——bg-others

    一 其他进程 Archiver (ARCn) Oneor more archiver processes copy the redo log files to archival storage whe ...

  8. 从零开始学C++之继承(二):继承与构造函数、派生类到基类的转换

    一.不能自动继承的成员函数 构造函数 析构函数 =运算符 二.继承与构造函数 基类的构造函数不被继承,派生类中需要声明自己的构造函数. 声明构造函数时,只需要对本类中新增成员进行初始化,对继承来的基类 ...

  9. 结构-行为-样式-Css笔记

     0.常见的行级元素和块级元素: 行级元素:div,table,form ,ul,ol,p,h1-h6,hr; 块级元素:a,select,input,textarea,img,label,br,i, ...

  10. 驱动04.平台总线驱动模型——点亮LED灯

    1 平台总线的简介 平台总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver.总线将设备和驱动绑定,在系统每注册一个设备的时候,会寻找与之匹配的 ...