Codeforces Round #341 Div.2 B. Wet Shark and Bishops
题意:处在同一对角线上的主教(是这么翻译没错吧= =)会相互攻击 求互相攻击对数
由于有正负对角线 因此用两个数组分别保存每个主教写的 x-y 和 x+y
然后每个数组中扫描重复数字k ans加上kC2就行了
wa了两发的原因是没考虑到如果整个数组都是重复的 那要最后额外加一次
#include <cstdio> #include <cmath> #include <cstring> #include <queue> #include <vector> #include <algorithm> #define INF 0x3f3f3f3f #define mem(str,x) memset(str,(x),sizeof(str)) #define STOP puts("Pause"); using namespace std; typedef long long LL; int n, node[200010], node2[200010]; LL ans; int main() { ans = 0; scanf("%d", &n); for(int i = 1; i <= n; i++){ int x, y; scanf("%d%d", &x, &y); node[i] = x - y; node2[i] = x + y; } sort(node + 1, node + 1 + n); sort(node2 + 1, node2 + 1 + n); LL count = 1; for(int i = 2; i <= n; i++){ if(node[i] == node[i-1]) count++; else{ ans += (count * (count - 1) / 2); count = 1; } } ans += (count * (count - 1) / 2); count = 1; for(int i = 2; i <= n; i++){ if(node2[i] == node2[i-1]) count++; else{ ans += (count * (count - 1) / 2); count = 1; } } ans += (count * (count - 1) / 2); printf("%I64d\n", ans); return 0; }