codeforces 659F F. Polycarp and Hay(并查集+bfs)
题目链接:
4 seconds
512 megabytes
standard input
standard 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.
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.
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.
2 3 35
10 4 9
9 9 7
YES
7 0 7
7 7 7
4 4 50
5 9 1 1
5 1 1 5
5 1 5 5
5 5 7 1
YES
5 5 0 0
5 0 0 5
5 0 5 5
5 5 5 0
2 4 12
1 1 3 1
1 6 2 4
NO
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矩阵,里面的值只能减小不能增大,要求是否可以得到连在一块的且和为k的连通块,而且这里面至少有一个的值没变;
思路:
先按从大到小的顺序排序,然后再把挨着的用并查集连在一块,同时更新这个集里面的元素个数,等操作到这个元素的值*这个元素所属集的元素个数>=k&&k能整除元素的值的时候就是能输出答案的时候了;再用bfs找到那些要求的地方输出就行,注意使用并查集要路径压缩,否则会tle;
AC代码:
/*
2014300227 659F - 62 GNU C++11 Accepted 1044 ms 33596 KB
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+;
int p[N],a[][],num[N],n,m,ans[][],vis[][];
int dir[][]={,-,,,,,-,};
ll k;
struct node
{
int hi,x,y;
};
node po[N];
int cmp(node u,node v)
{
return u.hi>v.hi;
}
int findset(int x)
{
if(x==p[x])return x;
return p[x]=findset(p[x]);
}
void same(int x,int y)
{
int fx=findset(x),fy=findset(y);
if(fx!=fy)
{
p[fx]=fy;
num[fy]+=num[fx];
num[fx]=;
}
}
int print(node r)
{
memset(vis,,sizeof(vis));
node temp;
int nu=;
queue<node>qu;
qu.push(r);
vis[r.x][r.y]=;
while(!qu.empty())
{
node to=qu.front();
qu.pop();
for(int i=;i<;i++)
{
int px=to.x+dir[i][],py=to.y+dir[i][];
if(px<||px>n||py<||py>m)continue;
if(a[px][py]>=r.hi&&nu<k/r.hi&&vis[px][py]==)
{
temp.hi=a[px][py];
temp.x=px;
temp.y=py;
qu.push(temp);
vis[px][py]=;
nu++;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(vis[i][j])
{
printf("%d ",r.hi);
}
else printf("0 ");
}
printf("\n");
} }
int main()
{
int cnt=;
cin>>n>>m>>k;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
po[cnt].hi=a[i][j];
po[cnt].x=i;
po[cnt].y=j;
cnt++;
}
}
for(int i=;i<N;i++)
{
p[i]=i;
num[i]=;
}
sort(po+,po+cnt+,cmp);
for(int i=;i<cnt;i++)
{ for(int j=;j<;j++)
{
int fx=po[i].x+dir[j][],fy=po[i].y+dir[j][]; if(fx<=||fx>n||fy<=||fy>m)continue;
if(a[fx][fy]>=po[i].hi)
{
same((fx-)*m+fy,(po[i].x-)*m+po[i].y);
}
}
if(k%po[i].hi==){
int fa=findset((po[i].x-)*m+po[i].y);
ll s=(ll)num[fa]*(ll)po[i].hi;
if(s>=k)
{
cout<<"YES"<<"\n";
print(po[i]);
return ;
}
}
}
printf("NO\n"); return ;
}
codeforces 659F F. Polycarp and Hay(并查集+bfs)的更多相关文章
- 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 ...
- Codeforces 659F Polycarp and Hay 并查集
链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- 【17.69%】【codeforces 659F】Polycarp and Hay
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- codeforces #541 F Asya And Kittens(并查集+输出路径)
F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...
- 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E
http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 977E:Cyclic Components(并查集)
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...
随机推荐
- ios 视图的旋转及应用
有时候,需要做出如下图所示的效果,这就需要用到视图的旋转了 1.首先将旋转的值由角度转换为弧度: #define degreesToRadinas(x) (M_PI * (x)/180.0) 注:M_ ...
- 数组方式使用jQuery对象
一. 使用jQuery选择器获取结果是一个jQuery对象.然而,jQuery类库会让你感觉你正在使用一个定义了索引和长度的数组.在性能方面,建议使用简单的for或者while循环来处理,而不是$.e ...
- C# Winform 运行异常 CefSharp.core.dll 找不到指定的模块
C# Winform开发中使用了CefSharp,之前在VS2012中运行很正常,今天换了一台Windows XP 打开VS2010 运行时,发生异常:System.IO.FileNotFoundEx ...
- vscode Js 插件 Jshint 的配置
vscode这款编辑器让人用起来很舒服,但是刚刚入手的童鞋可能会对其插件的安装产生一些恐惧,虽然vscode提供了插件的搜索和安装,但是其中一些插件是需要一些软件或者包之类的东西做支撑的,并不是在vs ...
- [转]Linux shell中的那些小把戏
我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧.是的,我能记住一些命令,但是肯定不会只在特定的任务上使用一次,所以我就开始在我的Dropbox账号里用文本 ...
- webapi设置一个Action同时支持get和post请求
代码如下: [AcceptVerbs("GET", "POST")] public HttpResponseMessage Http([FromUri]Prox ...
- JVM、垃圾回收、内存调优、常见參数
一.什么是JVM JVM是Java Virtual Machine(Java虚拟机)的缩写.JVM是一种用于计算设备的规范.它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现 ...
- 深入理解Java 8 Stream
Java 8中新增了Stream,主要是lambda表达式的应用,其链式调用简洁,用于高效表达集合操作. 先对Stream的使用做了解,参照blog. (1) 生成Stream的方式 主要有以下几种 ...
- 【PHP开发】用curl向https发请求时的35号错误
放了个假发现以前写的程序的模拟登陆不管用了,中间输出,发现curl向https发请求时没有返回数据,输出错误信息,得到: curl_errno($ch) -----> 35 curl_error ...
- VSCode 配置python
https://code.visualstudio.com/docs/?dv=win 下载64位的vscode 底下有python插件