codeforces 651C(map、去重)

题目链接:http://codeforces.com/contest/651/problem/C

思路:结果就是计算同一横坐标、纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
const int N = 2e5 + 5;
map <int,int> x,y;
map <pii,int> s;
map <int,int> :: iterator it1;
map <pii,int> :: iterator it2;
int main()
{
    int n;
    scanf("%d",&n);
    ll ans = 0;
    for(int i = 1; i <= n; i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        x[a]++,y[b]++;
        s[pii(a,b)]++;
    }
    for(it1 = x.begin(); it1 != x.end(); it1++)
        ans += (ll)(it1->second)*(it1->second - 1) >> 1;
    for(it1 = y.begin(); it1 != y.end(); it1++)
        ans += (ll)(it1->second)*(it1->second - 1) >> 1;
    for(it2 = s.begin(); it2 != s.end(); it2++)
        ans -= (ll)(it2->second)*(it2->second - 1) >> 1;
    printf("%I64d
",ans);
    return 0;
}



原文地址:https://www.cnblogs.com/westwind1005/p/5975179.html