Teacher Bo
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 103 Accepted Submission(s): 62
Problem Description
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
Source
2016 Multi-University Training Contest 3
题目大意:能否找到四个点: (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得A,B之间曼哈顿距离等于C,D之间曼哈顿距离,BC可以表示同一个点。
思路:
1、首先,有可能有重点,那么第一步,排序去重,对给出的点,进行排序后,去重。
2、简单分析,去掉重点之后点个数我们设定为N,即使点N的数量级不小,我们N^2可能会超时。但是也不能否认有这样一个事实:这么些个点的最大曼哈顿距离情况数不会超过2M个,因为距离原点最远的点也就是(M,M),其曼哈顿距离为2M,那么我们即使N^2比较大,但是因为没有重复点存在的情况下,如果有曼哈顿距离相等的情况,我们就跳出去了。如果没有曼哈顿距离相等的情况,也就是显然N^2中的曼哈顿距离种类数也没有达到2M+1个,所以,其时间复杂度并非N^2,而是min(N^2,2M).
3、那么我们在去重点之后,直接暴力求曼哈顿距离即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct zuobiao
{
int x,y;
}a[100050];
struct zuobiao2
{
int x,y;
}aa[100050];
int dis[200000];
int cmp(zuobiao aaa,zuobiao bbb)
{
if(aaa.x!=bbb.x)return aaa.x<bbb.x;
else return aaa.y<bbb.y;
}
int abs(int tmp)
{
if(tmp<0)return -tmp;
else return tmp;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dis,0,sizeof(dis));
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
int cont=0;
int i=1;
int pre=0;
aa[cont].x=a[0].x;
aa[cont++].y=a[0].y;
while(i<n)
{
if(a[i].x==a[pre].x&&a[i].y==a[pre].y)
{
i++;
}
else
{
aa[cont].x=a[i].x;
aa[cont++].y=a[i].y;
pre=i;
i++;
}
}
int ok=0;
for(int i=0;i<cont;i++)
{
for(int j=i+1;j<cont;j++)
{
if(dis[abs(a[j].x-a[i].x)+abs(a[j].y-a[i].y)]==0)
{
dis[abs(a[j].x-a[i].x)+abs(a[j].y-a[i].y)]=1;
}
else
{
ok=1;break;
}
}
}
if(ok==1)printf("YES\n");
else printf("NO\n");
}
}