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代码:
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e6+;
int n,m=;double x[maxn],y[maxn];
const double eps=1e-;
double Eulc(double a1,double b1,double a2,double b2){
return sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
}
double Manh(double a1,double b1,double a2,double b2){
return abs(a1-a2)+abs(b1-b2);
}
int main(){
cin>>n;
for(int i=;i<n;++i)cin>>x[i]>>y[i];
for(int i=;i<n-;++i){
for(int j=i+;j<n;++j){
if(abs(Eulc(x[i],y[i],x[j],y[j])-Manh(x[i],y[i],x[j],y[j]))<eps)m++;
}
}
cout<<m<<endl;
return ;
}
最新文章
- Qt4.8.5在ARM9上的移植
- IDL数组计算
- js 滚动 学习
- linux关于文件的那些事儿
- 猿题库 iOS 客户端架构设计
- python第十二天-----RabbitMQ
- php_curl.dll libssh2.dll 始终无法加载的原因 及解决办法
- Web Api2 用户认证模板解析---本地用户
- gulp入门
- cf C. Sereja and Algorithm
- oracle中clob字段的使用
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
- php 利用Gd库添加文字水印乱码的问题及解决方案
- python拼接multipart/form-data类型post请求格式
- BootStrap小知识随笔
- EF(EntityFramework)与mysql使用,取数据报错,linq实体映射错误
- Python爬虫html解析工具beautifulSoup在pycharm中安装及失败的解决办法
- 用GDB调试Segmentation 段错误【转】
- C++标准模板库(STL)和容器
- 快速排序——PowerShell版