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
input: standard input
output: 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.
样例
input
2 3 35
10 4 9
9 9 7
output
YES
7 0 7
7 7 7
题意
给你一块n*m的草地,每块草地高度为h[i][j]。
现在你要选择一根草的高度,将其他的草削成它那么高,或是直接拔掉。从而使最后剩下的草高度一致,且所它们属于同一个连通分量,且它们的高度和等于k。
如果存在多个满足的情况,任意输出一种
题解
把所有的草按高度从高到矮插到田里去,用并查集维护连通分量的大小,直到满足条件为止。
代码
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 1111;
typedef __int64 LL;
struct Node {
int val, x, y;
Node(int val, int x, int y) :val(val), x(x), y(y) {}
bool operator < (const Node& tmp)const {
return val > tmp.val;
}
};
int mat[maxn][maxn];
int n, m;
LL k,ans;
int f[maxn][maxn],siz[maxn][maxn];
const int base = 10000;
int find(int v) {
int x = v / base, y = v%base;
return f[x][y] = f[x][y] == v ? v : find(f[x][y]);
}
const int dx[] = { -1,1,0,0 };
const int dy[] = { 0,0,-1,1 };
bool solve(int val, int x, int y) {
mat[x][y] = val; siz[x][y] = 1;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (mat[tx][ty]) {
int anc = find(tx*base + ty);
if(anc!=f[x][y]){
siz[x][y] += siz[anc / base][anc%base];
f[anc / base][anc%base] = f[x][y];
}
}
}
if (k%val==0&&k/val<=siz[x][y]) return true;
return false;
}
int vis[maxn][maxn];
void dfs(int x, int y,int &cnt) {
vis[x][y] = 1; cnt++;
if (cnt >= k / ans) return;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (!vis[tx][ty] && mat[tx][ty]) {
dfs(tx, ty, cnt);
}
if (cnt >= k / ans) return;
}
}
void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = i*base + j;
}
}
memset(mat, 0, sizeof(mat));
memset(siz, 0, sizeof(siz));
memset(vis, 0, sizeof(vis));
}
int main() {
scanf("%d%d%I64d", &n, &m, &k);
init();
vector<Node> arr;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x; scanf("%d", &x);
arr.push_back(Node(x, i, j));
}
}
sort(arr.begin(), arr.end());
int su = 0;
for (int i = 0; i < arr.size(); i++) {
if (solve(arr[i].val, arr[i].x, arr[i].y)) {
ans=arr[i].val;
int cnt = 0;
dfs(arr[i].x, arr[i].y,cnt);
su = 1;
break;
}
}
if (su) {
puts("YES");
for (int i = 1; i <= n; i++) {
for (int j = 1; j < m; j++) {
if (vis[i][j]) printf("%d ", ans);
else printf("0 ");
}
if (vis[i][m]) printf("%d\n", ans);
else printf("0\n");
}
}
else {
puts("NO");
}
return 0;
}
Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集的更多相关文章
- 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 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 #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...
随机推荐
- tools安装
1.ruby安装 下载安装包 勾选中间一个 2.sass 安装 转换TB镜像 $ gem sources --remove https://rubygems.org/$ gem sources - ...
- C# 私人笔记
.ADO.NET 连接数据库的模版 string constr = "data source=127.0.0.1\\mysql2008;database=dbtest;integrated ...
- 移植FastBlur模糊算法至SDL
FastBlur是Android标配的模糊算法,这也在当时引起了一股毛玻璃热潮.IOS7就采用了此算法(这有抄袭Android之嫌,因为Android1.5就在标库中加入了此函数).算法效率很高,这也 ...
- 版本控制工具--GIT 基本命令(1)
一.安装GIT,在官网上下载安装即可(下面模拟环境是window7) 二.基本操作: 1.创建GIT库: ①先使用mkdir命令创建一个空目录,再使用git init将该目录变成GIT库,会在该目录下 ...
- Php 输出语句
输出语句 echo 示例: print(); 示例: 只能输出标量数据类型,无法输出布尔false print_r(); print_r可以以比较容易理解的方式显示数据 示例: var_dump(); ...
- 通过命令行连接oracle数据库/进入sql plus
1.直接打开SQL Plus登录,需要用户名和密码 开始->程序->Oracle->应用程序开发->sqlplus 我的电脑是“开始”->“Oracle - OraDb1 ...
- Linux下面对于VIM编辑器的代码折叠使用与screen
VIM设置代码折叠 1. 折叠方式 可用选项 'foldmethod' 来设定折叠方式:set fdm=*****.有 6 种方法来选定折叠: manual 手工 ...
- const和#define的区别
在刷题的时候经常遇到定义全局常量我一般都是用#define(可能是因为很少接触const的原因) 在昨天做到51nod1082时照常暴力用#define定义最大.可是提交超时..... 后来看他人写的 ...
- php数组去重的函数代码
php中数组去重的小例子. 代码如下: <?php /** * 数组去重复的小函数 * by www.jbxue.com */ function assoc_unique($arr ...
- mysql实例---sql语句中使用@变量
本文介绍下,在mysql语句中使用@变量的一个例子,学习下这个特殊变量的用法,有需要的朋友参考下吧. 要求: 计算用户距上次访问的天数,根据imei号区分不同的用户,如果时间段内只有一次访问则为0. ...