HDU 5762 Teacher Bo 鸽巢原理

  题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762

  题目描述: 问n个点对儿中有没有两个点曼哈顿距离相同、N , M <= 1e5

  解题思路: 给你的N是唬人的, 由于坐标绝对值最大距离是M, 所以曼哈顿最大距离就是2*M, 也就是说最多2*M+1次绝对会出解, 复杂度为O(M)

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,-0x3f,sizeof(a))
#define fi(n) for(i=0;i<n;i++)
#define fj(m) for(j=0;j<m;j++)
#define sca(x) scanf("%d",&x)
#define ssca(x) scanf("%s",x)
#define scalld(x) scanf("%I64d",&x)
#define print(x) printf("%d\n", x)
#define printlld(x) printf("%I64d\n",x)
#define de printf("=======\n")
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long long ll;
using namespace std;

const int maxn = 1e5+100;
int x[maxn];
int y[maxn];
int vis[2*maxn];

int main() {
    int t;
    sca(t);
    while( t-- ) {
        int n, m;
        mem0(vis);
        sca(n);
        sca(m);
        for( int i = 1; i <= n; i++ ) {
            sca(x[i]), sca(y[i]);
        }
        int flag = 0;
        for( int i = 1; i < n; i++ ) {
            for( int j = i+1; j <= n; j++ ) {
                int dis = abs(x[i]-x[j])+abs(y[i]-y[j]);
                if( vis[dis] ) {
                    flag = 1;
                    break;
                }
                else vis[dis] = 1;
            }
            if( flag ) break;
        }
        if( flag ) printf( "YES\n" );
        else printf( "NO\n" );
    }
    return 0;
}
View Code

  思考: 一开始看题没明白题目的意思.....还想求最近点对儿和最远点对儿.......再用鸽巢.......mdzz