zoukankan      html  css  js  c++  java
  • 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}) .
  • 相关阅读:
    在美国贩卖早餐的小摊贩
    随感
    业内人士称游资3年前开始准备炒作糖价
    许志:量化宽松在即 美元迎来关键一周
    9月17日  逾200亿资金净流出 农行轰然长阴 好笑
    9月热钱流入环比加速 多为投机性资金
    错过了多次捞钱的机会
    20101012 期货盘面暴跌,亏损持仓
    致歉申明,现在《微电子工程师》可以正常下载了
    SemiId半导体型号识别器1.0发布
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5728456.html
Copyright © 2011-2022 走看看