HDU 5762 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,A≠CorB≠D) 
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.(T≤50)

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

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

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

最多有2*m種曼哈頓距離,通過n計算出當前點能組成的曼哈頓距離總數,如果大於2m說明必然存在兩對滿足要求的點,否則暴力求解即可。

ac代碼:

#include <bits/stdc++.h>
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define rep(i,j,k) for(int i = j; i <= k; i++)
using namespace std;
struct point{
    int x,y;
}a[100006];
map<int, int>mp;
int main(){
    int t, m, n;
    cin  >> t;
    while(t--){
        cin >> n >> m;
        mp.clear();
        rep(i,1,n) {
            scanf("%d%d",&a[i].x, &a[i].y);
        }
        bool f = false;
        long long tmp = n*(n-1)/2;
        if(tmp > 2*m) f = true;
        else
        rep(i,1,n)
            rep(j,i+1,n){
                int l = abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y);
                mp[l]++;
                if(mp[l]>1) {
                    f = true;
                    break;
                }
            }
        puts(f?"YES":"NO");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章