您的位置:首页 > 其它

Codeforces #345 div1 A. Watchmen 数学

2016-03-09 19:01 405 查看

题目

题目链接:http://codeforces.com/contest/650/problem/A

题目来源:http://codeforces.com/contest/650

简要题意:求平面上欧几里得距离等于曼哈顿距离的点对个数。

题解

列出等式可以发现两点必然xx坐标相同或yy坐标相同或重合。

用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: