Codeforces 659F Polycarp and Hay【思维+并查集+Bfs】
2017-10-16 12:23
399 查看
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.
Input
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.
Output
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.
Examples
input
output
input
output
input
output
Note
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的一个矩阵,现在让我们找到一个点(x,y),然后在找到一个联通块,使得联通块中的所有点的值都大于等于a【x】【y】,并且联通块中的所有值被选定后都是a【x】【y】,问我们能否找到一个联通块,使得其和为K.
思路:
①因为和a【x】【y】联通的部分,都必须大于等于a【x】【y】,那么我们不妨将整个N*M的矩阵线性存储起来,然后将其按照价值从大到小排序。
②那么我们考虑一个值一个值的加入,那么对于后来加入的值来讲,如果之前有位子a【x】【y】和当前位子a【i】【j】是相邻的,那么对应我们就可以将两个点连通起来,提到连通,自然我们能够想到并查集,那么我们用并查集维护一个联通块中点的个数即可。
当存在:K%a【i】【j】==0&&和a【i】【j】连通的点的个数cnt*a【i】【j】>=K的时候,就是有解的时候。
③问题需要输出路径,那么我们再Bfs一下即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define ll __int64
ll n,m;
ll k;
struct node
{
ll x,y,val;
}a[1005000],now,nex;
ll vis[1050][1050];
ll aa[1050][1050];
ll num[1050][1050];
ll f[1005000];
ll sum[1005000];
ll fx[4]={0,0,1,-1};
ll fy[4]={1,-1,0,0};
ll find(ll a)
{
ll r=a;
while(f[r]!=r)
r=f[r];
ll i=a;
ll j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(ll a,ll b)
{
ll A,B;
A=find(a);
B=find(b);
if(A!=B)
{
f[B]=A;
sum[A]+=sum[B];
}
}
ll cmp(node a,node b)
{
return a.val>b.val;
}
void Bfs(ll sx,ll sy,ll numm)
{
ll tot=1;
memset(vis,0,sizeof(vis));
queue<node>s;
now.x=sx,now.y=sy;
vis[sx][sy]=1;
s.push(now);
while(!s.empty())
{
now=s.front();
s.pop();
for(ll i=0;i<4;i++)
{
nex.x=now.x+fx[i];
nex.y=now.y+fy[i];
if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m)
{
if(vis[nex.x][nex.y]==0&&aa[nex.x][nex.y]>=aa[sx][sy])
{
if(tot==numm)return ;
vis[nex.x][nex.y]=1;
tot++;
s.push(nex);
}
}
}
}
}
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
{
ll cnt=0;
for(ll i=1;i<=n*m;i++)f[i]=i,sum[i]=1;
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
cnt++;
ll x;
scanf("%I64d",&x);
num[i][j]=cnt;
aa[i][j]=x;
a[cnt].x=i;
a[cnt].y=j;
a[cnt].val=x;
}
}
ll flag=0;
sort(a+1,a+1+cnt,cmp);
for(ll i=1;i<=cnt;i++)
{
for(ll j=0;j<4;j++)
{
ll xx=a[i].x+fx[j];
ll yy=a[i].y+fy[j];
if(xx>=1&&yy>=1&&xx<=n&&yy<=m)
{
if(aa[xx][yy]>=aa[a[i].x][a[i].y])
merge(num[xx][yy],num[a[i].x][a[i].y]);
}
}
ll contt=sum[find(num[a[i].x][a[i].y])];
ll val=a[i].val;
if(contt*val>=k&&k%val==0)
{
flag=val;
Bfs(a[i].x,a[i].y,k/val);
break;
}
}
if(flag>0)
{
printf("YES\n");
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
if(vis[i][j]==1)
{
printf("%d ",flag);
}
else printf("0 ");
}
printf("\n");
}
}
else printf("NO\n");
}
}
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.
Input
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.
Output
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.
Examples
input
2 3 35 10 4 9 9 9 7
output
YES 7 0 7 7 7 7
input
4 4 50 5 9 1 1 5 1 1 5 5 1 5 5 5 5 7 1
output
YES 5 5 0 0 5 0 0 5 5 0 5 5 5 5 5 0
input
2 4 12 1 1 3 1 1 6 2 4
output
NO
Note
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的一个矩阵,现在让我们找到一个点(x,y),然后在找到一个联通块,使得联通块中的所有点的值都大于等于a【x】【y】,并且联通块中的所有值被选定后都是a【x】【y】,问我们能否找到一个联通块,使得其和为K.
思路:
①因为和a【x】【y】联通的部分,都必须大于等于a【x】【y】,那么我们不妨将整个N*M的矩阵线性存储起来,然后将其按照价值从大到小排序。
②那么我们考虑一个值一个值的加入,那么对于后来加入的值来讲,如果之前有位子a【x】【y】和当前位子a【i】【j】是相邻的,那么对应我们就可以将两个点连通起来,提到连通,自然我们能够想到并查集,那么我们用并查集维护一个联通块中点的个数即可。
当存在:K%a【i】【j】==0&&和a【i】【j】连通的点的个数cnt*a【i】【j】>=K的时候,就是有解的时候。
③问题需要输出路径,那么我们再Bfs一下即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define ll __int64
ll n,m;
ll k;
struct node
{
ll x,y,val;
}a[1005000],now,nex;
ll vis[1050][1050];
ll aa[1050][1050];
ll num[1050][1050];
ll f[1005000];
ll sum[1005000];
ll fx[4]={0,0,1,-1};
ll fy[4]={1,-1,0,0};
ll find(ll a)
{
ll r=a;
while(f[r]!=r)
r=f[r];
ll i=a;
ll j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(ll a,ll b)
{
ll A,B;
A=find(a);
B=find(b);
if(A!=B)
{
f[B]=A;
sum[A]+=sum[B];
}
}
ll cmp(node a,node b)
{
return a.val>b.val;
}
void Bfs(ll sx,ll sy,ll numm)
{
ll tot=1;
memset(vis,0,sizeof(vis));
queue<node>s;
now.x=sx,now.y=sy;
vis[sx][sy]=1;
s.push(now);
while(!s.empty())
{
now=s.front();
s.pop();
for(ll i=0;i<4;i++)
{
nex.x=now.x+fx[i];
nex.y=now.y+fy[i];
if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m)
{
if(vis[nex.x][nex.y]==0&&aa[nex.x][nex.y]>=aa[sx][sy])
{
if(tot==numm)return ;
vis[nex.x][nex.y]=1;
tot++;
s.push(nex);
}
}
}
}
}
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
{
ll cnt=0;
for(ll i=1;i<=n*m;i++)f[i]=i,sum[i]=1;
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
cnt++;
ll x;
scanf("%I64d",&x);
num[i][j]=cnt;
aa[i][j]=x;
a[cnt].x=i;
a[cnt].y=j;
a[cnt].val=x;
}
}
ll flag=0;
sort(a+1,a+1+cnt,cmp);
for(ll i=1;i<=cnt;i++)
{
for(ll j=0;j<4;j++)
{
ll xx=a[i].x+fx[j];
ll yy=a[i].y+fy[j];
if(xx>=1&&yy>=1&&xx<=n&&yy<=m)
{
if(aa[xx][yy]>=aa[a[i].x][a[i].y])
merge(num[xx][yy],num[a[i].x][a[i].y]);
}
}
ll contt=sum[find(num[a[i].x][a[i].y])];
ll val=a[i].val;
if(contt*val>=k&&k%val==0)
{
flag=val;
Bfs(a[i].x,a[i].y,k/val);
break;
}
}
if(flag>0)
{
printf("YES\n");
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
if(vis[i][j]==1)
{
printf("%d ",flag);
}
else printf("0 ");
}
printf("\n");
}
}
else printf("NO\n");
}
}
相关文章推荐
- 【CodeForces 351A】Jeff and Rounding(思维)
- codeforces 384C Milking cows(脑洞+思维)
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces 722C(并查集 + 思维)
- Codeforces 763B Timofey and rectangles(四色定理)(思维)
- CodeForces - 673B(思维)
- Codeforces 407B Long Path【思维+dp】
- CodeForces - 739A Alyona and mex ( 思维 且题意难理解 值得重做 )
- CodeForces - 767A Snacktower(模拟+思维)
- CodeForces 598A Tricky Sum(思维)
- Codeforces 620E New Year Tree【DFS序+线段树区间染色+二进制思维+位运算】
- Codeforces 534B Covered Path【有点奇怪的思维+贪心】
- Codeforces 519D - A and B and Interesting Substrings (思维)
- codeforces 897B. Chtholly's request(思维or构造)
- CodeForces 144C Anagram Search(思维)
- Codeforces 485D Maximum Value【思维+技巧枚举】
- CodeForces 547D Mike and Fish 思维
- Codeforces 303A Lucky Permutation Triple【思维】
- 【CodeForces】233B - Non-square Equation(思维)
- 【codeforces 233 B Non-square Equation】+ 思维