标签:first coder iostream diagonal 内存限制 eve pos turn 输出
There are N queens in an infinite chessboard. We say two queens may attack each other if they are in the same vertical line, horizontal line or diagonal line even if there are other queens sitting between them.
Now given the positions of the queens, find out how many pairs may attack each other?
The first line contains an integer N.
Then N lines follow. Each line contains 2 integers Ri and Ci indicating there is a queen in the Ri-th row and Ci-th column.
No two queens share the same position.
For 80% of the data, 1 <= N <= 1000
For 100% of the data, 1 <= N <= 100000, 0 <= Ri, Ci <= 1000000000
One integer, the number of pairs may attack each other.
5 1 1 2 2 3 3 1 3 3 1
10
用 map 记录下每行每列每条斜线上有多少个 queen,最后统计一下即可。
1 #include <cstdio> 2 #include <iostream> 3 #include <set> 4 #include <vector> 5 #include <map> 6 7 using namespace std; 8 9 10 int ccc(int t) 11 { 12 return t*(t-1)/2; 13 } 14 15 int main() 16 { 17 int N; 18 cin >> N; 19 int r, w; 20 map<int, int>s1, s2, s3, s4; 21 while (N--) 22 { 23 cin >> r >> w; 24 s1[r]++; 25 s2[w]++; 26 s3[r - w]++; 27 s4[r + w]++; 28 } 29 int ans = 0; 30 for (auto it : s1) 31 { 32 ans += ccc(it.second); 33 } 34 for (auto it : s2) 35 { 36 ans += ccc(it.second); 37 } 38 for (auto it : s3) 39 { 40 ans += ccc(it.second); 41 } 42 for (auto it : s4) 43 { 44 ans += ccc(it.second); 45 } 46 printf("%d", ans); 47 return 0; 48 }
hihoCoder 第255周 hiho一下 Queen Attack
标签:first coder iostream diagonal 内存限制 eve pos turn 输出
原文地址:https://www.cnblogs.com/zhangchaosd/p/10888052.html