[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\n",ans);
        
        return 0;
    
}

 

转载于:https://www.cnblogs.com/evenbao/p/9715159.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值