Q - Euclid in Manhattan
Posted acgoto
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Q - Euclid in Manhattan相关的知识,希望对你有一定的参考价值。
Desciption
Consider a set of n points in a 2-D plane with integer coordinates. There are various ways of measuring distance between two points such as Euclidean , Manhattan , Chebyshev distance. These distances have important application , one of which is chess.
Consider that the ith point is located at (xi , yi). We want to find the number of pairs(i, j) such that the Euclidean distance between the points i and j is equal to the Manhattan distance between the same two points, i.e. Euclidean distance(i, j) = Manhattan distance(i, j).
√((xi ? xj )^2 + (yi ? yj )^2) - is called Euclidean distance
| xi ? xj | + | yi ? yj | - is called Manhattan distance
Note - All the n points given are considered different, even if they share the same coordinates.
Input
First line contains n, number of points in the plane Each of the following n lines contains two integers xi , yi
Output
Print the total number of such pairs.
Example
Input: 3 1 1 7 5 1 5 Output: 2
Input: 6 0 0 0 1 0 2 -1 1 0 1 1 1 Output: 11
解题思路:注意判断两个小数是否相等,一般采用做差法且其差值小于一个很小的精度,则视这两个小数相等。这题没给出n的范围,暴力O(n^2)水过。
AC代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 const int maxn=1e6+5; 6 int n,m=0;double x[maxn],y[maxn]; 7 const double eps=1e-8; 8 double Eulc(double a1,double b1,double a2,double b2){ 9 return sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2)); 10 } 11 double Manh(double a1,double b1,double a2,double b2){ 12 return abs(a1-a2)+abs(b1-b2); 13 } 14 int main(){ 15 cin>>n; 16 for(int i=0;i<n;++i)cin>>x[i]>>y[i]; 17 for(int i=0;i<n-1;++i){ 18 for(int j=i+1;j<n;++j){ 19 if(abs(Eulc(x[i],y[i],x[j],y[j])-Manh(x[i],y[i],x[j],y[j]))<eps)m++; 20 } 21 } 22 cout<<m<<endl; 23 return 0; 24 }
以上是关于Q - Euclid in Manhattan的主要内容,如果未能解决你的问题,请参考以下文章
欧几里得 & 拓展欧几里得算法 解说 (Euclid & Extend- Euclid Algorithm)