CodeForces - 650A Watchmen (握手定理)
来源:互联网 发布:妄想代理人知乎 编辑:程序博客网 时间:2024/05/21 19:13
题意:
给出 N 个坐标点,求有多少对点的坐标满足|xi - xj| + |yi - yj|=.。
思路:
推导后发现 当两点的一个坐标相同时满足条件。
所以求出每种 X 坐标和每种 Y 坐标的重复次数,利用握手定理。得满足题意的点对数为
其中 q 为 X 坐标的种类, X[i] 为每种 X 坐标重复的个数,p 为 y 坐标的种类, Y[i] 为每种 Y 坐标重复的个数。
但当两点相同时,会出现重复计算的情况,又由握手定理得
ANS =
其中 w 为重复点的种类, Z[i] 为每种点的重复次数
代码:
#include <bits/stdc++.h>using namespace std;const int MAXN=2e5+100;long long n,x,y;long long ans;map <long long,long long> cx,cy;map <pair<long long,long long>,long long >cp;pair <long,long> sav[MAXN];int main(){ ios::sync_with_stdio(false); while(cin>>n){ ans=0; cx.clear();cy.clear(); cp.clear(); for(int i=0;i<n;i++){ cin>>x>>y; sav[i]=make_pair(x,y); cx[x]++; cy[y]++; cp[sav[i]]++; } for(int i=0;i<n;i++){ long long temp=cp[sav[i]]; if(temp){ ans+=cx[sav[i].first]*(cx[sav[i].first]-1)/2; ans+=cy[sav[i].second]*(cy[sav[i].second]-1)/2; ans-=temp*(temp-1)/2; cp[sav[i]]=0; cx[sav[i].first]=0; cy[sav[i].second]=0; } } cout<<ans<<endl; }}
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, thei-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 i and 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.
31 17 51 5
2
60 00 10 2-1 10 11 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.
- CodeForces - 650A Watchmen (握手定理)
- CodeForces 650A Watchmen
- Watchmen(Codeforces 650A)
- CodeForces 650 A.Watchmen(map)
- codeforces 650A Watchmen 【数学】
- codeforces--650A Watchmen(math)
- Codeforces 650A Watchmen(数学,算法优化)
- Codeforces 650A Watchmen 【数学 + 二分】
- 【1】【组合数学】CodeForces 650A Watchmen
- Codeforces Round #345 (Div. 1) & CodeForces 650A Watchmen
- 650A.Watchmen
- CF 650A Watchmen
- Codeforces #345 div1 A. Watchmen 数学
- A. Watchmen
- Codeforces 651C - Watchmen
- CodeForces 651 C Watchmen
- codeforces 651C Watchmen
- codeforces 651C. Watchmen
- 天黑黑 (Standard IO)
- 程序员的那些需要反省的地方
- hihoCoder--1039 字符消除
- Java再初学
- [BZOJ2115][Wc2011] Xor(dfs+高斯消元求线性基+贪心)
- CodeForces - 650A Watchmen (握手定理)
- 洛谷OJ P1162填涂颜色
- 洛谷1803 凌乱的yyy(第29题)
- XHTML简介
- linux配置网络
- OIBH杯第三次模拟赛(普及组)Problem 3 : maxsum 最大约数和
- ubuntu16.04引导加密
- 斯坦福机器学习——异常检测
- 绘图组件总结