HDU 5762 Teacher Bo

Teacher Bo

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


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
 
Source
 
 
 
解析:因为曼哈顿距离最多有2*M种,可以枚举点对之间的曼哈顿距离,并用一个bool数组记录每种距离是否出现过,枚举时如果发现重复就输出"YES",否则输出"NO"。乍一看,时间复杂度是O(N2),实际上最多循环min{2*M, N*(N-1)/2}次,时间复杂度为O(min{M, N2})。
 
 
 
#include <bits/stdc++.h>
using namespace std;

pair<int, int> p[100005];
bool vis[200005];

void solve(int n, int m)
{
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            int dis = abs(p[i].first-p[j].first)+abs(p[i].second-p[j].second);
            if(vis[dis]){
                puts("YES");
                return;
            }
            else
                vis[dis] = true;
        }
    }
    puts("NO");
}

int main()
{
    int t, n, m;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; ++i)
            scanf("%d%d", &p[i].first, &p[i].second);
        solve(n, m);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/inmoonlight/p/5711779.html