Codeforces Round #341 (Div. 2) --B. Wet Shark and Bishops

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.

好坑的一道题,关键就是没读懂啥意思:

大体题意:

给你n个坐标,如果坐标位于相同的对角线上,则会相互攻击,问相互攻击的对数。

注意:

这里的对角线,不是让你在这n个坐标中寻找一个矩形,在判断是否对角线。。。这太麻烦了,

这里的对角线就是和网格正方形对角线平行的直线,

说白了就是过这个点做斜率为1和-1的直线,这就是它的两根对角线!T T

思路:

这样思路就很清晰了,我不太适应他这种建系方式,我就把坐标先转换了一下。转换成左下角为原点的坐标系!

然后用两个map,m1.m2,记录斜率为1和-1时的截距。

最后统计截据,套一下公式   个数 = num*(num-1)/2就行了!


代码如下:


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int f(int n){
    return n*(n-1)/2;
}
int main()
{
    int n;
    ios::sync_with_stdio(false);
    while(cin >> n){
            map<int,int>m1,m2;
            for (int i = 0; i < n; ++i){
            int x1,y1,u,v;
            cin >> x1 >> y1;
            u = 1000-y1;
            v = 1000-x1;
            if (!m1.count(v-u))m1[v-u]=0;
            m1[v-u]++;
            if (!m2.count(v+u))m2[v+u]=0;
            m2[v+u]++;
            }
            int sum = 0;
            for (int i = -1000; i < 2001; ++i){
                if (m1.count(i))sum+=f(m1[i]);
                if (m2.count(i))sum+=f(m2[i]);
            }
            cout << sum << endl;
    }
    return 0;
}


你可能感兴趣的:(C语言,codeforces)