Teacher Bo
Time Limit: 4000/2000 MS (Java/Others)
题目链接:
http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1011&cid=706
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
2
3 10
1 1
2 2
3 3
4 10
8 8
2 3
3 3
4 4
Sample Output
YES
NO
题目分析:
考虑一种暴力,每次枚举两两点对之间的曼哈顿距离,并开一个桶记录每种距离是否出现过,如果某次枚举出现了以前出现的距离就输 YES ,否则就输 NO .
注意到曼哈顿距离只有 O(M)种,根据鸽笼原理,上面的算法在 O(M)O(M) 步之内一定会停止.所以是可以过得.
一组数据的时间复杂度 O(min{N^2,M})
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int x[maxn],y[maxn],flag;
int app[maxn];
int n,m;
int man(int xx1,int yy1,int xx2,int yy2)
{
return abs(xx1-xx2)+abs(yy1-yy2);
}
void solve()
{
int cnt=0;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{ int dist=man(x[i],y[i],x[j],y[j]);
if(!app[dist])
app[dist]=1;
else{
flag=1;
goto endd;
}
}
endd:;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
scanf("%d",&t);
while(t--)
{
memset(app,0,sizeof(app));
flag=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
solve();
if(flag)puts("YES");
else
puts("NO");
}
return 0;
}