Teacher BoBo is a geography teacher in the school.One day in his class,he marked N points in the map,the i-th point is at (Xi,Yi).He wonders,whether there is a tetrad (A,B,C,D)(A<B,C<D,A≠CorB≠D) such that the manhattan distance between A and B is equal to the manhattan distance between C and D.
If there exists such tetrad,print “YES”,else print “NO”.
Input
First line, an integer T. There are T test cases.(T≤50)
In each test case,the first line contains two intergers, N, M, means the number of points and the range of the coordinates.(N,M≤105).
Next N lines, the i-th line shows the coordinate of the i-th point.(Xi,Yi)(0≤Xi,Yi≤M).
Output
T lines, each line is “YES” or “NO”.
Sample Input
2
3 10
1 1
2 2
3 3
4 10
8 8
2 3
3 3
4 4
Sample Output
YES
NO
解题思路
首先用知道不能暴力,1e5的数据还是多组,肯定超时。要是知道这个manhattan单词,曼哈顿距离意思就是A到B的距离为
|x1-x2|+|y1-y2|,而题上又给了m坐标的范围,我们知道n+1的糖果分给n个人总会有人得到2个糖果,曼哈顿距离最大是2m,我们只要能求到2m+1个点就可以结束循环,这样最多循环2e5次,再算上多组也不会超时。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
map<ll,ll> mp;//用来看看有没有出现曼哈顿距离相等的情况
struct node
{
int x,y;
}e[maxn];//用来存点
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
mp.clear();
st.clear();
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&e[i].x,&e[i].y);
}
int cnt=0;
bool flag=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cnt++;
if(cnt>(2*m))//当大于2*m就可以停止循环
{
flag=1;
break;
}
ll xx=abs(e[i].x-e[j].x);
ll yy=abs(e[i].y-e[j].y);
ll ss=xx+yy;//曼哈顿距离
if(mp[ss]==0)
mp[ss]++;
else
{
flag=1;
break;
}
}
if(flag) break;
}
if(!flag) printf("NO\n");
else printf("YES\n");
}
return 0;
}