Codeforces Round #141 (Div. 2)
2014-03-31 19:42
225 查看
题目链接
B. Two Tables
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You've got two rectangular tables with sizes na × ma and nb × mb cells.
The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th
row and the j-th column, as ai, j;
we will define the element of the second table, located at the intersection of the i-th row and the j-th
column, as bi, j.
We will call the pair of integers (x, y) a shift of the second table
relative to the first one. We'll call the overlap factor of the shift (x, y)value:
where the variables i, j take only such values, in which the expression ai, j·bi + x, j + y makes
sense. More formally, inequalities 1 ≤ i ≤ na, 1 ≤ j ≤ ma, 1 ≤ i + x ≤ nb, 1 ≤ j + y ≤ mb must
hold. If there are no values of variables i, j, that satisfy the given inequalities, the value of the sum is considered equal to
0.
Your task is to find the shift with the maximum overlap factor among all possible shifts.
Input
The first line contains two space-separated integers na, ma (1 ≤ na, ma ≤ 50) —
the number of rows and columns in the first table. Then na lines
contain ma characters
each — the elements of the first table. Each character is either a "0", or a "1".
The next line contains two space-separated integers nb, mb (1 ≤ nb, mb ≤ 50) —
the number of rows and columns in the second table. Then follow the elements of the second table in the format, similar to the first table.
It is guaranteed that the first table has at least one number "1". It is guaranteed that the second table has at least one number "1".
Output
Print two space-separated integers x, y (|x|, |y| ≤ 109) —
a shift with maximum overlap factor. If there are multiple solutions, print any of them.
Sample test(s)
input
output
input
output
题意:求x和y使得
最大。
由于数据非常小,直接暴力就行。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
char aa[55][55],bb[55][55];
int a[55][55],b[55][55];
int na,ma,nb,mb,x,y,i,j,k,xx,yy;
int main()
{
cin>>na>>ma;
for(i=0;i<na;i++)
{
scanf("%s",aa[i]);
for(j=0;j<ma;j++)
{
a[i+1][j+1]=aa[i][j]-'0';
}
}
cin>>nb>>mb;
for(i=0;i<nb;i++)
{
scanf("%s",bb[i]);
for(j=0;j<mb;j++)
{ b[i+1][j+1]=bb[i][j]-'0';//cout<<b[i+1][j+1]<<" ";
}
}
int sum=0,ans=0,xx=-nb,yy=-mb,nn,mm;
for(x=-na;x<=nb;x++)
for(y=-ma;y<=mb;y++)
{
for(i=1;i<=na;i++)
for(j=1;j<=ma;j++)
{
nn=i+x;mm=j+y;
if(nn>0&&nn<=nb&&mm>0&&mm<=mb&&a[i][j]==1&&b[nn][mm]==1)
sum++;
}
if(ans<sum)
{ ans=sum;xx=x;yy=y;
//cout<<ans<<"**"<<xx<<"**"<<yy<<endl;
}
sum=0;
}
printf("%d %d\n",xx,yy);
return 0;
}
B. Two Tables
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You've got two rectangular tables with sizes na × ma and nb × mb cells.
The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th
row and the j-th column, as ai, j;
we will define the element of the second table, located at the intersection of the i-th row and the j-th
column, as bi, j.
We will call the pair of integers (x, y) a shift of the second table
relative to the first one. We'll call the overlap factor of the shift (x, y)value:
where the variables i, j take only such values, in which the expression ai, j·bi + x, j + y makes
sense. More formally, inequalities 1 ≤ i ≤ na, 1 ≤ j ≤ ma, 1 ≤ i + x ≤ nb, 1 ≤ j + y ≤ mb must
hold. If there are no values of variables i, j, that satisfy the given inequalities, the value of the sum is considered equal to
0.
Your task is to find the shift with the maximum overlap factor among all possible shifts.
Input
The first line contains two space-separated integers na, ma (1 ≤ na, ma ≤ 50) —
the number of rows and columns in the first table. Then na lines
contain ma characters
each — the elements of the first table. Each character is either a "0", or a "1".
The next line contains two space-separated integers nb, mb (1 ≤ nb, mb ≤ 50) —
the number of rows and columns in the second table. Then follow the elements of the second table in the format, similar to the first table.
It is guaranteed that the first table has at least one number "1". It is guaranteed that the second table has at least one number "1".
Output
Print two space-separated integers x, y (|x|, |y| ≤ 109) —
a shift with maximum overlap factor. If there are multiple solutions, print any of them.
Sample test(s)
input
3 2 01 10 00 2 3 001 111
output
0 1
input
3 3 000 010 000 1 1 1
output
-1 -1
题意:求x和y使得
最大。
由于数据非常小,直接暴力就行。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
char aa[55][55],bb[55][55];
int a[55][55],b[55][55];
int na,ma,nb,mb,x,y,i,j,k,xx,yy;
int main()
{
cin>>na>>ma;
for(i=0;i<na;i++)
{
scanf("%s",aa[i]);
for(j=0;j<ma;j++)
{
a[i+1][j+1]=aa[i][j]-'0';
}
}
cin>>nb>>mb;
for(i=0;i<nb;i++)
{
scanf("%s",bb[i]);
for(j=0;j<mb;j++)
{ b[i+1][j+1]=bb[i][j]-'0';//cout<<b[i+1][j+1]<<" ";
}
}
int sum=0,ans=0,xx=-nb,yy=-mb,nn,mm;
for(x=-na;x<=nb;x++)
for(y=-ma;y<=mb;y++)
{
for(i=1;i<=na;i++)
for(j=1;j<=ma;j++)
{
nn=i+x;mm=j+y;
if(nn>0&&nn<=nb&&mm>0&&mm<=mb&&a[i][j]==1&&b[nn][mm]==1)
sum++;
}
if(ans<sum)
{ ans=sum;xx=x;yy=y;
//cout<<ans<<"**"<<xx<<"**"<<yy<<endl;
}
sum=0;
}
printf("%d %d\n",xx,yy);
return 0;
}
相关文章推荐
- 纯js操作div移动
- position:absolute; div居中
- Codeforces Round #133 (Div. 2) A题
- Codeforces Round #246 (Div. 2)
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- Codeforces Round #250 (Div. 1) A. The Child and Toy 水题
- Codeforces Round #358 (Div. 2) B Alyona and Mex(水题)
- div滚动条在最低端
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- Codeforces Round #420 (Div. 2) Okabe and El Psy Kongroo[矩阵快速幂+细节处理]
- javascript + div 做的滚动条
- div屏幕居中
- Codeforces Round #187 (Div. 2) B. Sereja and Array
- Codeforces Round #209 (Div. 2) C. Prime Number
- div+css 常用三种自动适应宽度
- 【Codeforces Round #327 (Div. 2)E】【最短路】 Three States 块联通
- Codeforces Round #360 (Div. 2) C D E
- Codeforces Round #387 (Div. 2)D. Winter Is Coming(复杂贪心)
- Codeforces Round #426 (Div. 2) D. The Bakery (线段树)
- Codeforces Round #467 (Div. 2)D. Sleepy Game(记忆化搜索+状态压缩)