codeforces 650A Watchmen
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).
They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .
The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman iand watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.
Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).
Some positions may coincide.
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.
3
1 1
7 5
1 5
2
6
0 0
0 1
0 2
-1 1
0 1
1 1
11
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
题意:问两个点之间的距离按照|1 - 7| + |1 - 5| 和这种方法算,结果相同的对数 分析:求横坐标相同或者纵坐标相同的点的对数即可,(注意减去横坐标纵坐标都相同的点)
#include<stdio.h> #include<string.h> #include<cstdio> #include<string> #include<math.h> #include<algorithm> #define LL long long #define PI atan(1.0)*4 #define DD double #define MAX 200200 #define mod 100 #define dian 1.000000011 #define INF 0x3f3f3f using namespace std; struct node { LL x,y; }s[MAX]; bool cmp(node a,node b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } bool cmp1(node c,node d) { if(c.y==d.y) return c.x<d.x; return c.y<d.y; } int main() { LL n,m,j,i,k,t; LL sum,sum1,sum2,sum3; while(scanf("%lld",&n)!=EOF) { for(i=0;i<n;i++) scanf("%lld%lld",&s[i].x,&s[i].y); sum=0; sum1=sum2=sum3=1; sort(s,s+n,cmp); for(i=0;i<n;i++) { j=i; sum1=0; while(j<n&&s[i].x==s[j].x) { sum1++; j++; } sum+=sum1*(sum1-1)/2; i=j-1; } sort(s,s+n,cmp1); for(i=0;i<n;i++) { sum2=0; j=i; while(s[i].y==s[j].y) { sum2++; j++; } sum+=sum2*(sum2-1)/2; i=j-1; } for(i=0;i<n;i++) { sum3=0; j=i; while(s[i].x==s[j].x&&s[i].y==s[j].y) { sum3++; j++; } sum-=sum3*(sum3-1)/2; i=j-1; } printf("%lld\n",sum); } return 0; }