您的位置:首页 > 其它

Codeforces Round #341 (Div. 2)(B)数学

2016-02-03 22:38 405 查看
B. Wet Shark and Bishops

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 test(s)

input
5
1 1
1 5
3 3
5 1
5 5


output
6


input
3
1 1
2 3
3 5


output
0


Note

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.

题意:给你一些坐标,问在这些坐标的对角线上有多少可以攻击的对手,只能从上向下的攻击

题解:观察可以发现,一条对角线上的和其实就是一个等差数列,那么只要把每条对角线上的点统计一下,根据公式求和就好了,这里可以使用循环扫描一遍求得点的个数,也可以使用一种神奇而又简单的数学规律:

在每一条对角线上的值,他的x+y,x-y都是固定且唯一的值。其实这个原理很好证明,由斜截式 y = kx+b可知,显然x-y(或y-x)是一个常数,那么我们只需要开2个一维数组就可以表示所有的线段了,注意x-y可能是负数,所以数组开为最大和的2倍在加上一个最大和防止负数就好了。

其实我是想吐槽这一题真的有毒呀



#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 5000int line1
,line2
;
int half=N/2;
int f(int x)
{
return ((x-1)*x)/2;
}
int main()
{
memset(line1,0,sizeof(line1));
memset(line2,0,sizeof(line2));
int n,x,y;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
line1[x+y]++;
line2[x-y+half]++;
}
int ans=0;
for(int i=0;i<N;i++)
{
ans+=f(line1[i]);
ans+=f(line2[i]);
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: