HDU5762 Teacher Bo 多校联赛第三场1011

来源:互联网 发布:守望先锋手机数据查询 编辑:程序博客网 时间:2024/05/18 20:08

Teacher Bo

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1532    Accepted Submission(s): 593


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
23 101 12 23 34 108 82 33 34 4
 

Sample Output
YESNO
 

Statistic | Submit | Clarifications | Back
题解
曼哈顿距离  l=|x1-x2|+|y1-y2|

考虑一种暴力,每次枚举两两点对之间的曼哈顿距离,并开一个桶记录每种距离是否出现过,如果某次枚举出现了以前出现的距离就输 YESYES ,否则就输 NONO .

注意到曼哈顿距离只有 O(M)O(M) 种,根据鸽笼原理,上面的算法在 O(M)O(M) 步之内一定会停止.所以是可以过得.

一组数据的时间复杂度 O(\min{N^2,M})O(min{N2,M}) .

方法一

#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>using namespace std;int dp[2000005];struct node{    int x,y;}a[100005];int cmp(node a,node b){    if(a.x!=b.x)        return a.x<b.x;    else        return a.y<b.y;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m,i,j;        memset(dp,0,sizeof(dp));        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);        for(i=1;i<n;i++)        {            if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y)            {                for(j=i+1;j<n;j++)                {                    a[j]=a[j-1];                }                n--;                i--;            }        }        int flag=0;        for(i=0;i<n;i++)        {            if(flag==1)                break;            for(j=i+1;j<n;j++)            {                int ss=abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y);                dp[ss]++;                if(dp[ss]>=2)                {                    flag=1;                    break;                }            }        }        if(flag==1)            printf("YES\n");        else            printf("NO\n");    }}
第二种方法

#include<bits/stdc++.h>using namespace std;int dp[2000005];struct node{    int x,y;} a[100005];int cmp(node a,node b){    if(a.x!=b.x)        return a.x<b.x;    else        return a.y<b.y;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m,i,j;        scanf("%d%d",&n,&m);        set<int>u;        for(int i=0; i<n; i++)        {            scanf("%d%d",&a[i].x,&a[i].y);        }        sort(a,a+n,cmp);        for(i=1; i<n; i++)        {            if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y)            {                for(j=i+1; j<n; j++)                {                    a[j]=a[j-1];                }                n--;                i--;            }        }        int flag=0;        for(i=0; i<n; i++)        {            if(flag==1)                break;            for(j=i+1; j<n; j++)            {                int ss=abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y);                if(u.find(ss)!=u.end())                {                    flag=1;                    break;                }                u.insert(ss);            }        }        if(flag==1)            printf("YES\n");        else            printf("NO\n");    }}





0 0