CodeForces 659F Polycarp and Hay
并查集,$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的更多相关文章
- Codeforces 659F Polycarp and Hay 并查集
链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...
- Codeforces 659F Polycarp and Hay【BFS】
有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发...好吧,其实也就这一次... 小白说的对,还是代码能力不足... 非常不足... 题目链接: http://codefo ...
- codeforces 659F . Polycarp and Hay 搜索
题目链接 遍历每个点, 如果这个点的值能被k整除并且k/a[i][j]后小于等于n*m, 那么就对这个点进行搜索. 将这个点加入队列, 将周围的所有大于等于这个点的值的点也加入队列. 不断重复, 直到 ...
- 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 ...
- 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 ...
- 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 ...
- 【17.69%】【codeforces 659F】Polycarp and Hay
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- Codeforces 723C. Polycarp at the Radio 模拟
C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- codeforces 727F. Polycarp's problems
题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...
随机推荐
- iis与 asp.net管道(asp.net应用程序什么周期)
iis5和iis6.0下面,把iis的管道和asp.net管道进行了隔离,带来了一些局限和不足. 比如: 1.iis和asp.net之间有一些相同的操作.比如:身份认证. 2.动态文件和静态文件的 ...
- VS2010下C/C++连接MySql数据库的方法
1. 新建一个C++控制台程序 2. 选择项目 CMySql属性 3. 选择配置属性 C/C++ 常规 附加包含目录 4. 添加包含目录C:\Program Files\MySQL\Connector ...
- 记录Spring.net学习中遇到的各种问题
1.由于项目中使用了spring.net作为IOC容器,所以看了下相应的博客,熟悉一下这方面的内容,参照博客为博客园刘冬的博客系列: 博客地址:http://www.cnblogs.com/GoodH ...
- 支付宝移动支付开发详细教程服务端采用.net mvc webapi(C#)
转自:http://www.kwstu.com/ArticleView/netmvc_201511132005431321 最近开发手机app需要实现移动支付功能,由于考虑支付安全将支付宝生成签名写到 ...
- ASP.NET4.5Web API及非同步程序开发系列
ASP.NET4.5Web API及非同步程序开发系列 认识ASP.NET WEB API 他的前身为WCF WEB API用于协助WCF支持RestFul.现在集成进ASP.NET,正式更名为ASP ...
- STM8S TIM4 初始化设置
#define TIM4_DIV1 (unsigned char)0 #define TIM4_DIV2 (unsigned char)1 #define TIM4_DIV4 (unsigned ch ...
- 2013Esri全球用户大会之解读Web GIS
1 什么是Web GIS,它跟我有什么关系? Web GIS是传递GIS功能的一种新方式,在Esri把GIS作为平台进行实现的战略方向中位于中心位置.Web GIS为用户随时随地访问和使用地理信息提供 ...
- centos 6.4 x64安装bugfree
第一步:下载xampp-linux-1.8.1.tar.gz [root@SVNMANAGER ~]# tar -zxvf xampp-linux-1.8.1.tar.gz -C /opt [root ...
- linux 下把Caps_Lock 映射成Ctrl
我用的是debian 7,以前是gnome桌面,系统里就有改变键盘布局的设置.之前gnome 崩了一次,就换成了openbox ,稍微配置了一下也可以很好使用. 可以参考这篇文章 5分钟openbox ...
- BIOS详解:什么是BIOS ?BIOS的作用?CMOS及其与BIOS的关系?
1.什么是BIOS ? BIOS是英文Basic Input Output System的缩略语,直译过来后中文名称就是基本输入输出系统.它的全称应该是ROM-BIOS,意思是只读存储器基本输入输出系 ...