hdu5762Teacher Bo+曼哈顿距离

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

问是否存在 (A,B,C,D)(A< B,C< D,A≠CorB≠D)使得AB曼哈顿距离等于CD曼哈顿距离。d(i,j)=|xi-xj|+|yi-yj|。
其实这是一个暴力题。这里并没有说ABCD都不一样,只说了,A≠CorB≠D,所以说只需要暴力求解两点之间的曼哈顿距离,只要这个距离曾经出现过,那么就存在状态满足条件。那么问题来了两重循环为啥不超时?我们可以看看。。曼哈顿的范围是多少?没错0到2×M.。最大也就2×10^5。。。所以由于抽屉原理,那么2×10^5+1以内至少会存在两个相等的曼哈顿距离。。所以直接跳出去就好了。。如果不可以也可以在2×10^5范围里扫描完所有点。。
强行map增加了复杂度,不过也能A,大大说直接一个数组,更简单。orz!

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
#define LL long long
struct Point{
    int x,y;
    Point(){};
    Point(int x,int y):x(x),y(y){};
}point[100005];

map< int,int >Q;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d %d",&point[i].x,&point[i].y);
        Q.clear();
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                int temp=abs(point[i].x-point[j].x)+abs(point[i].y-point[j].y);
                //printf("%d\n",temp);
                if(Q.find(temp)!=Q.end()){
                    ok=true;
                    break;
                }
                else Q[temp]++;
            }
            if(ok==true) break;
        }
        if(ok==true) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值