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 ...
随机推荐
- css3 背景记
css3 背景 css背景主要包括五个属性: background-color background-color:transparent || <color> 用来设置元素的背景颜色,默认 ...
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- iOS开发那些事--性能优化–内存泄露问题的解决(转)
内存泄漏问题的解决 内存泄漏(Memory Leaks)是当一个对象或变量在使用完成后没有释放掉,这个对象一直占有着这块内存,直到应用停止.如果这种对象过多内存就会耗尽,其它的应用就无法运行.这个问题 ...
- 基于asp.net MVC 的服务器和客户端的交互(二)之获取Oauth 2.0认证权限
基本Web API的ASP.NET的Oauth2认证 增加Token额外字段 增加Scope授权字段 持久化Token 设计Token的时间间隔 刷新Token后失效老的Token 自定义验证[重启I ...
- jQuery API 3.1.0 速查表-打印版
jQuery API 3.1.0 速查表-打印图,(API来自:http://jquery.cuishifeng.cn/index.html)
- Windows7 下配置添加ASP功能
按照如下顺序添加 1.控制面板-程序-打开或关闭Windows功能 2.Internet信息服务-万维网服务-应用程序开发功能 3.勾选ASP 和ASP.net选项 确定后安装完毕即可支持.
- java比.net优美的一个小地方
用了四年的.net,今年转做java,内心一直吐槽java的烦琐,今天发现了一个java值得我为之点赞的地方 java的枚举居然可以这么玩,废话不多,上demo package com.sunline ...
- (转)IDG副总裁楼军:顶级VC青睐什么样的创业者
学习能力是创业者的第一能力 创业者首先要有格局观和很强的学习能力. 具体什么意思?比如说去年IDG投了一个做C2C平台的海淘项目,创始人之前其实是帮他爱人做海淘代购.他爱人是一个代购买手,赚得还不错, ...
- bzoj 1005 HNOI2008 明明的烦恼
这题做的我欲哭无泪啊…… 我已经没心情多说啥了…… 高精度T啊!我太弱啊!改了一天啊!还不如滚粗啊! 想好式子在写啊! 能用高精度乘单精度就不要用高精度乘高精度啊! 能用高精度除单精度就不要用 ...
- [java学习笔记]java语言核心----面向对象之构造函数
1.构造函数概念 特点: 函数名与类名相同 不用定义返回值类型 没有具体的返回值 作用: 给对象进行初始化 注意: 默认构造函数 多个构造函数是以重载出现的 一个类中如果 ...