【CodeForces】[621B]Wet Shark and Bishops

这里写图片描述

刚开始认为是对角线
然后发现只要是斜率为1或-1的线的都可以互相攻击

所以可以用数组记录截距来统计在同一直线上的个数

#include<stdio.h>
#include<string.h>
int a[2020],b[2020];
int main() {
    int n;
    while(scanf("%d",&n)!=EOF) {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0; i<n; i++) {
            int x,y;
            scanf("%d %d",&x,&y);
            if(x>y)
                a[1000+x-y]++;
            else
                a[y-x]++;
            b[x+y]++;
        }
        int res=0;
        for(int i=0; i<2020; i++) {
            if(a[i]) {
                res+=(a[i]-1)*a[i]/2;
            }
            if(b[i]) {
                res+=(b[i]-1)*b[i]/2;
            }
        }
        printf("%d
",res);
    }
    return 0;
}

题目地址:[621B]Wet Shark and Bishops

原文地址:https://www.cnblogs.com/BoilTask/p/12569556.html