Codeforces Round #341 (Div. 2)-B. Wet Shark and Bishops(暴搜+组合)

来源:互联网 发布:长沙理工大学网络认证 编辑:程序博客网 时间:2024/06/01 09:36
B. Wet Shark and Bishops
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.

Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

Input

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.

Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output

Output one integer — the number of pairs of bishops which attack each other.

Sample test(s)
input
51 11 53 35 15 5
output
6
input
31 12 33 5
output
0
Note

In the first sample following pairs of bishops attack each other: (1, 3)(1, 5)(2, 3)(2, 4)(3, 4) and (3, 5). Pairs (1, 2)(1, 4)(2, 5)and (4, 5) do not attack each other because they do not share the same diagonal.

思路:

    这题就是1000*1000的复杂度,因为我只要计算左斜线与右斜线就行了。这题一开始我是想到从左上角求右斜线,而右上角求左斜线的。但是写了出来却变成了在右下角了,结果wa8.最后统计到每条斜线有多少个元素就可以用C(n,m)求出答案了。

AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<cstdio>#include<cmath>#include<set>using namespace std;#define CRL(a) memset(a,0,sizeof(a))typedef unsigned __int64 LL;typedef  __int64 ll;const int T = 103000;const int mod = 1000000007;int Map[1010][1010];bool vis[1010][1010];ll comb(double a,double b){double cm = 1.0;while(b>0){cm*=(a--)/(b--);}return cm+0.5;}int main(){#ifdef zsc    freopen("input.txt","r",stdin);#endifint n,m,i,j,k,u,v;int row,col;while(~scanf("%d",&n)){memset(vis,false,sizeof(vis));memset(Map,0,sizeof(Map));row = col = 1000;for(i=0;i<n;++i){scanf("%d%d",&u,&v);Map[u][v] = 1;}ll cnt=0;vector<ll> ve;for(i=1;i<=row;++i){for(j=1;j<=col;++j){cnt=0;if(!vis[i][j]&&Map[i][j]){for(k=0;i+k<=row&&j+k<=col;++k){vis[i+k][j+k] = true;if(Map[i+k][j+k]){cnt++;}}}if(cnt>1){ve.push_back(cnt);}}}memset(vis,false,sizeof(vis));for(i=1;i<=row;++i){for(j=col;j>=1;--j){cnt=0;if(!vis[i][j]&&Map[i][j]){for(k=0;i+k<=row&&j-k>=0;++k){vis[i+k][j-k] = true;if(Map[i+k][j-k]){cnt++;}}}if(cnt>1){ve.push_back(cnt);}}}cnt = 0;for(i=0;i<ve.size();++i){cnt += comb(ve[i],2);}printf("%I64d\n",cnt);}    return 0;}


1 0