Codeforces #345 div1 A. Watchmen 数学

题目

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值