CodeForces 650A Watchmen

两种距离相等,必然是在一条竖直线上或者一条水平线上。重复的再减一下即可。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <stack>
using namespace std;
typedef long long LL;
const int maxn=2e5+9;
const int inf=1e9+9;

struct II
{
    LL x,y;
}num[maxn];

int cmp1(II a,II b);
int cmp2(II a,II b);
int cmp3(II a,II b);

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d%I64d",&num[i].x,&num[i].y);
    }
    sort(num+1,num+1+n,cmp1);
    LL ans=0;
    LL tmp=1;
    for(int i=2;i<=n;i++)
    {
        if(num[i].x==num[i-1].x)
            tmp++;
        else
        {
            ans+=tmp*(tmp-1)/2;
            tmp=1;
        }
    }
    ans+=tmp*(tmp-1)/2;

    sort(num+1,num+1+n,cmp2);
    tmp=1;
    for(int i=2;i<=n;i++)
    {
        if(num[i].y==num[i-1].y)
            tmp++;
        else
        {
            ans+=tmp*(tmp-1)/2;
            tmp=1;
        }
    }
    ans+=tmp*(tmp-1)/2;

    sort(num+1,num+1+n,cmp3);
    LL nn=1;
    for(int i=2;i<=n;i++)
    {
        if(num[i].x==num[i-1].x && num[i].y==num[i-1].y)
            nn++;
        else
        {
            ans-=(nn)*(nn-1)/2;
            nn=1;
        }
    }
    ans-=(nn)*(nn-1)/2;
    printf("%I64d\n",ans);

    return 0;
}

int cmp3(II a,II b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}

int cmp2(II a,II b)
{
    return a.y<b.y;
}

int cmp1(II a,II b)
{
    return a.x<b.x;
}

 

posted @ 2016-03-08 08:38  Fighting_Heart  阅读(230)  评论(0编辑  收藏  举报