CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)

CodeForces - 621B
Wet Shark and Bishops
Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.

Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

Input

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.

Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output

Output one integer — the number of pairs of bishops which attack each other.

Sample Input

Input
5
1 1
1 5
3 3
5 1
5 5
Output
6
Input
3
1 1
2 3
3 5
Output
0

Hint

In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.

//题意:输入n,在输入n个点的坐标

表示在1000*1000的矩阵中有n条鲨鱼,它们所在的坐标即为所给的。如果在矩阵的对角线上有多条鲨鱼,那么这条对角线上的多条鲨鱼就会打架(并且即使它们不相邻,他们也会打架),问总共有多少对打架的鲨鱼。

//思路:

因为它是一个1000*1000的正方形,所以它的对角线条数总共有4000条,分别是y=x+b的两千条和y=-x+b的两千条.因为它们分别都是平行的,所以它们的b(截距)是唯一的。在输入的时候分别对它们的截距b的值进行记录累加就行了。

求出对应截距的个数后,就可以计算每条对角线上的打架鲨鱼的对数了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define N 3010
#define M 1000000007
using namespace std;
int a[N];
int b[N];
int main()
{
	int n;
	int x,y;
	int i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int xx;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			a[x+y]++;
			xx=y-x;
			if(xx<0)
				xx=(-xx+2000);
			b[xx]++;
		}
		int sum=0;
		for(i=0;i<=3000;i++)
		{
			sum+=(a[i]*(a[i]-1)/2);
			sum+=(b[i]*(b[i]-1)/2);
		}
		printf("%d\n",sum);
	}
	return 0;
}


 

你可能感兴趣的:(CodeForces - 621B Wet Shark and Bishops (数学几何&技巧))