HDU 5962 2016 多校合练Contest 3 Teacher 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,ACorBD) 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. (T50)

In each test case,the first line contains two intergers, N, M, means the number of points and the range of the coordinates. (N,M105).

Next N lines, the i-th line shows the coordinate of the i-th point. (Xi,Yi)(0Xi,YiM).
 


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
 估计上面也不会太认真看,我也是为了占行数。。。
题意:
搜索n个点,是否有曼哈顿距离相等的两组点。
这里如果直接暴力就是O(n^2)10^10超时,没有想到降复杂度的方法。于是我从数据范围下手。
如果有n个点,那么就有n*(n-1)条边。假设这些点都在一条直线上,(不再一条直线上曼哈顿距离相似度更高),且仅仅以相邻的点曼哈顿距离为有效距离且曼哈顿距离都不一样的,那么在m范围内,最多有i个点,i满足(i+1)*i/2<2*m。然后数据点的范围就降到m^(1/2),那么这样O(n^2)复杂度也不会超。
#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iterator>
#define N 200000
using namespace std;
struct node
{
    int x,y;
    bool operator<(const node&x)const{
        return this->x<x.x;
    }
};
map<node,int> g;
map<node,int>::iterator I;
map<node,int>::iterator J;
int f[N+10];
map<int,int> Map;
int main()
{
    //freopen("in.txt","r",stdin);

    int n,m;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        g.clear();
        Map.clear();
        memset(f,0,sizeof(f));
        cin>>n>>m;
        int fa=1;
        int k=0;
        int sum=0;
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);            
            node temp;
            temp.x=x;
            temp.y=y;
            g[temp]++;
            if(g[temp]==1)
            {                                
                f[k]=x+y;
                k++;
                sum++;
            }
        }
        int temp=sqrt(m*1.0);
        while(temp*temp<m)
            temp++;
        if(sum>temp*2)
        {
            printf("YES\n");
            continue;
        }
        fa=0;
        for(I=g.begin();I!=g.end();I++)
        {
            for(J=I;J!=g.end();J++)
            {
                if(J==I)
                    continue;
                Map[abs(I->first.x-J->first.x)+abs(I->first.y-J->first.y)]++;
                if(Map[abs(I->first.x-J->first.x)+abs(I->first.y-J->first.y)]>1)
                    fa=1;
                if(fa)
                    break;
            }
            if(fa)
                break;
        }
        if(fa)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}



 

猜你喜欢

转载自blog.csdn.net/yizhangbiao/article/details/52045487
今日推荐