[Codeforces 650A] Watchmen

[题目链接]

        https://codeforces.com/problemset/problem/650/A

[算法]

        显然 , 只有横坐标 / 纵坐标相等的点 , 才会满足 : .    = | xi - xj | + | yi - yj |

        如果有n个点的横 / 纵坐标相等 , 那么它们将会对答案产生n(n - 1) / 2的贡献

        不妨维护三个std :: map , 分别记录横坐标相同 , 纵坐标相同和重点的个数 

        时间复杂度 : O(NlogN)

[代码]

         

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;

int n;
long long ans;
map<int,int> a,b;
map<pair<int,int>,int> c;

template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

int main()
{
        
        read(n);
        for (int i = 1; i <= n; i++) 
        {
                int x , y;
                read(x); read(y);
                a[x]++; b[y]++;
                c[make_pair(x,y)]++;
        }
        for (map< int,int > :: iterator it = a.begin(); it != a.end(); it++)
        {
                int value = it -> second;
                if (value == 1) continue;
                ans += 1ll * value * (value - 1) / 2;        
        }
        for (map< int,int > ::iterator it = b.begin(); it != b.end(); it++)
        {
                int value = it -> second;
                if (value == 1) continue;
                ans += 1ll * value * (value - 1) / 2;
        }
        for (map< pair<int,int>,int > :: iterator it = c.begin(); it != c.end(); it++)
        {
                int value = it -> second;
                if (value == 1) continue;
                ans -= 1ll * value * (value - 1) / 2;
        }
        printf("%I64d
",ans);
        
        return 0;
    
}
原文地址:https://www.cnblogs.com/evenbao/p/9715159.html