• hdu 5762 Teacher Bo 曼哈顿路径


    Teacher Bo

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


    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
    #include<iostream>
    #include<stdio.h>
    #include<set>
    #include<math.h>
    using namespace std;
    const int maxx = 100005;
    set<int> hav;
    int px[maxx];
    int py[maxx];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            hav.clear();
            scanf("%d%d",&n,&m);
            for(int i=0; i<n; i++ )
            {
                scanf("%d%d",px+i,py+i);
            }
            int flag=0;
            for(int i=0; i<n-1; i++)
            {
                for(int j=i+1; j<n; j++)
                {
                    int dis=abs(px[j]-px[i])+abs(py[j]-py[i]);
                    if(hav.count(dis))
                    {
                        flag=1;
                        break;
                    }
                    else
                    {
                        hav.insert(dis);
                    }
                }
                if(flag)
                {
                    break;
                }
            }
            if(flag) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
    View Code
    考虑一种暴力,每次枚举两两点对之间的曼哈顿距离,并开一个桶记录每种距离是否出现过,如果某次枚举出现了以前出现的距离就输 YESYES ,否则就输 NONO .
    
    注意到曼哈顿距离只有 O(M)O(M) 种,根据鸽笼原理,上面的算法在 O(M)O(M) 步之内一定会停止.所以是可以过得.
    
    一组数据的时间复杂度 O(min{N^2,M})O(min{N^2​​ ,M}) .
  • 相关阅读:
    小禾满月了
    Gitlab-CI使用及.gitlab-ci.yml配置入门一篇就够了
    什么是CLI?
    什么是root帐户?
    Linux 的目录结构是怎样的?
    什么叫 CC 攻击?什么叫 DDOS 攻击?
    什么是 inode ?
    判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下?
    编写 Shell 程序,实现自动删除 50 个账号的功能,账号名为stud1 至 stud50 ?
    请问当用户反馈网站访问慢,如何处理?
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5728456.html
Copyright © 2020-2023  润新知