题目
题目链接:http://codeforces.com/contest/650/problem/A
题目来源:http://codeforces.com/contest/650
简要题意:求平面上欧几里得距离等于曼哈顿距离的点对个数。
题解
列出等式可以发现两点必然 x 坐标相同或
y 坐标相同或重合。用map去统计每行每列的个数然后记数。
由于重合的时候会同时在两次计算中出现,需要再减去,因而还要对每个点的个数记数。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
LL cal(LL x) {
return x * (x-1);
}
map<int, int> mapx, mapy;
map<PII, int> ma;
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
mapx[x]++;
mapy[y]++;
ma[make_pair(x, y)]++;
}
LL ans = 0;
for (auto it : mapx) {
ans += cal(it.se);
}
for (auto it : mapy) {
ans += cal(it.se);
}
for (auto it : ma) {
ans -= cal(it.se);
}
printf("%I64d\n", ans / 2);
return 0;
}