AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
2018-03-04 23:31
447 查看
Problem Statement
We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).
The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.
You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x−i|+|y−j| magic points.
You now have to take Q practical tests of your ability as a magical girl.
The i-th test will be conducted as follows:
Initially, a piece is placed on the square where the integer Li is written.
Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not Ri. The test ends when x=Ri.
Here, it is guaranteed that Ri−Li is a multiple of D.
For each test, find the sum of magic points consumed during that test.
Constraints
1≤H,W≤300
1≤D≤H×W
1≤Ai,j≤H×W
Ai,j≠Ax,y((i,j)≠(x,y))
1≤Q≤105
1≤Li≤Ri≤H×W
(Ri−Li) is a multiple of D.
Input
Input is given from Standard Input in the following format:
H W D
A1,1 A1,2 … A1,W
:
AH,1 AH,2 … AH,W
Q
L1 R1
:
LQ RQ
Output
For each test, print the sum of magic points consumed during that test.
Output should be in the order the tests are conducted.
Sample Input 1
Copy
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8
Sample Output 1
5
4 is written in Square (1,2).
6 is written in Square (3,3).
8 is written in Square (3,1).
Thus, the sum of magic points consumed during the first test is (|3−1|+|3−2|)+(|3−3|+|1−3|)=5.
Sample Input 2
4 2 3
3 7
1 4
5 2
6 8
2
2 2
2 2
Sample Output 2
0
0
Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.
Sample Input 3
5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13
Sample Output 3
0
5
0
题意:给出h*w的表格和数字,问从一个点到另一个点的的坐标间的距离是多少。
思路:简单的打表,只是这个题略有小坑,在边界处理上#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#define N 1005
using namespace std;
typedef long long ll;
int a[305][305];
struct node
{
int x;
int y;
}p[90005];
int ans[90005];
int main()
{
int h,w,d,len;
scanf("%d%d%d",&h,&w,&d);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++){
scanf("%d",&a[i][j]);
int n=a[i][j];
p
.x=i;
p
.y=j;
}
int q;
len=h*w;
scanf("%d",&q);
for(int i=1;i<=d;i++)
{
int l=i,r=len,sum=0;
ans[i]=0;
while(l<r)
{
sum=sum+abs(p[l].x-p[l+d].x)+abs(p[l].y-p[l+d].y);
l=l+d;
ans[l]=sum;
}
}
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ans[r]-ans[l]);
}
}这份代码会出现RE和WA,当时脑子发热,不明白RE的原因,后来同学发现了bug,当l=r-1,再循环一次l=r+d-1,超出数据范围,所以RE和WA
AC的代码#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#define N 1005
using namespace std;
typedef long long ll;
int a[305][305];
struct node
{
int x;
int y;
}p[90005];
int ans[90005];
int main()
{
int h,w,d,len;
scanf("%d%d%d",&h,&w,&d);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++){
scanf("%d",&a[i][j]);
int n=a[i][j];
p
.x=i;
p
.y=j;
}
int q;
len=h*w;
scanf("%d",&q);
for(int i=1;i<=d;i++)
{
int l=i,r=len,sum=0;
ans[i]=0;
while(l<=r-d)
{
sum=sum+abs(p[l].x-p[l+d].x)+abs(p[l].y-p[l+d].y);
l=l+d;
ans[l]=sum;
}
}
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ans[r]-ans[l]);
}
}
We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).
The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.
You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x−i|+|y−j| magic points.
You now have to take Q practical tests of your ability as a magical girl.
The i-th test will be conducted as follows:
Initially, a piece is placed on the square where the integer Li is written.
Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not Ri. The test ends when x=Ri.
Here, it is guaranteed that Ri−Li is a multiple of D.
For each test, find the sum of magic points consumed during that test.
Constraints
1≤H,W≤300
1≤D≤H×W
1≤Ai,j≤H×W
Ai,j≠Ax,y((i,j)≠(x,y))
1≤Q≤105
1≤Li≤Ri≤H×W
(Ri−Li) is a multiple of D.
Input
Input is given from Standard Input in the following format:
H W D
A1,1 A1,2 … A1,W
:
AH,1 AH,2 … AH,W
Q
L1 R1
:
LQ RQ
Output
For each test, print the sum of magic points consumed during that test.
Output should be in the order the tests are conducted.
Sample Input 1
Copy
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8
Sample Output 1
5
4 is written in Square (1,2).
6 is written in Square (3,3).
8 is written in Square (3,1).
Thus, the sum of magic points consumed during the first test is (|3−1|+|3−2|)+(|3−3|+|1−3|)=5.
Sample Input 2
4 2 3
3 7
1 4
5 2
6 8
2
2 2
2 2
Sample Output 2
0
0
Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.
Sample Input 3
5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13
Sample Output 3
0
5
0
题意:给出h*w的表格和数字,问从一个点到另一个点的的坐标间的距离是多少。
思路:简单的打表,只是这个题略有小坑,在边界处理上#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#define N 1005
using namespace std;
typedef long long ll;
int a[305][305];
struct node
{
int x;
int y;
}p[90005];
int ans[90005];
int main()
{
int h,w,d,len;
scanf("%d%d%d",&h,&w,&d);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++){
scanf("%d",&a[i][j]);
int n=a[i][j];
p
.x=i;
p
.y=j;
}
int q;
len=h*w;
scanf("%d",&q);
for(int i=1;i<=d;i++)
{
int l=i,r=len,sum=0;
ans[i]=0;
while(l<r)
{
sum=sum+abs(p[l].x-p[l+d].x)+abs(p[l].y-p[l+d].y);
l=l+d;
ans[l]=sum;
}
}
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ans[r]-ans[l]);
}
}这份代码会出现RE和WA,当时脑子发热,不明白RE的原因,后来同学发现了bug,当l=r-1,再循环一次l=r+d-1,超出数据范围,所以RE和WA
AC的代码#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#define N 1005
using namespace std;
typedef long long ll;
int a[305][305];
struct node
{
int x;
int y;
}p[90005];
int ans[90005];
int main()
{
int h,w,d,len;
scanf("%d%d%d",&h,&w,&d);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++){
scanf("%d",&a[i][j]);
int n=a[i][j];
p
.x=i;
p
.y=j;
}
int q;
len=h*w;
scanf("%d",&q);
for(int i=1;i<=d;i++)
{
int l=i,r=len,sum=0;
ans[i]=0;
while(l<=r-d)
{
sum=sum+abs(p[l].x-p[l+d].x)+abs(p[l].y-p[l+d].y);
l=l+d;
ans[l]=sum;
}
}
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ans[r]-ans[l]);
}
}
相关文章推荐
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 089 D - Practical Skill Test 边界处理
- AtCoder Beginner Contest 087 D - People on a Line 并查集(区间和问题)
- AtCoder Beginner Contest 054 ABCD题
- AtCoder Beginner Contest 084 C - Special Trains(思路)
- AtCoder Beginner Contest 073 ABC C++&&Python3
- AtCoder Beginner Contest 085 C Otoshidama(计算机整数乘法问题+方程求解)
- AtCoder Beginner Contest 091 D - Two Sequences
- 【AtCoder Beginner Contest 073 D】joisino's travel
- AtCoder Beginner Contest 069【A,水,B,水,C,数学,D,暴力】
- AtCoder Beginner Contest 069 D - Grid Coloring
- AtCoder Beginner Contest 087 D - People on a Line 并查集(区间和问题)
- AtCoder Beginner Contest 053 ABCD题
- AtCoder Beginner Contest 084 C - Special Trains(思路)
- AtCoder Beginner Contest 085 C Otoshidama(计算机整数乘法问题+方程求解)