zoukankan      html  css  js  c++  java
  • [HDU 2966]In case of failure

    Description

    To help their clients deal with faulty Cash Machines, the board of The Planar Bank has decided to stick a label expressing sincere regret and sorrow of the bank about the failure on every ATM. The very same label would gently ask the customer to calmly head to the nearest Machine (that should hopefully
    work fine).
    In order to do so, a list of two-dimensional locations of all n ATMs has been prepared, and your task is to find for each of them the one closest with respect to the Euclidean distance.

    Input

    The input contains several test cases. The very first line contains the number of cases t (t <= 15) that follow. Each test cases begin with the number of Cash Machines n (2 <= n <= 10^5). Each of the next n lines contain the coordinates of one Cash Machine x,y (0 <= x,y <=10^9) separated by a space. No two
    points in one test case will coincide.

    Output

    For each test case output n lines. i-th of them should contain the squared distance between the i-th ATM from the input and its nearest neighbour.

    Sample Input

    2 10 17 41 0 34 24 19 8 28 14 12 45 5 27 31 41 11 42 45 36 27 15 0 0 1 2 2 3 3 2 4 0 8 4 7 4 6 3 6 1 8 0 11 0 12 2 13 1 14 2 15 0

    Sample Output

    200 100 149 100 149 52 97 52 360 97 5 2 2 2 5 1 1 2 4 5 5 2 2 2 5

    题目大意

    求平面最近点对。

    题解

    裸的$KD-tree$。

    我们用线性结构来存储这颗树,其主要思想是将一段区间内的元素中间那个元素作为根节点,其余作为左右子树,用个决策函数来划分。

    询问的时候就比较暴力...其主要就是通过根节点以及其与子树的关系来优化搜索过程。

     1 //It is made by Awson on 2017.11.25
     2 #include <map>
     3 #include <set>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <queue>
     7 #include <stack>
     8 #include <cstdio>
     9 #include <string>
    10 #include <vector>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define LD long double
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Min(a, b) ((a) < (b) ? (a) : (b))
    19 #define dist(a, b, c, d) (sqr(a-c)+sqr(b-d))
    20 #define sqr(x) ((x)*(x))
    21 #define y1 yy
    22 #define count COUNT
    23 using namespace std;
    24 const int N = 1e5;
    25 
    26 int n, now;
    27 LL ans;
    28 struct tt {
    29   LL x[2];
    30   bool operator < (const tt &b) const {
    31     return x[now] < b.x[now];
    32   }
    33 }a[N+5], b[N+5];
    34 struct KD_tree {
    35   void build(int l, int r, int flag) {
    36     if (l >= r) return;
    37     now = flag;
    38     int mid = (l+r)>>1;
    39     nth_element(a+l, a+mid, a+r+1);
    40     build(l, mid-1, flag^1); build(mid+1, r, flag^1);
    41   }
    42   void query(int l, int r, int flag, tt p) {
    43     if (l > r) return;
    44     int mid = (l+r)>>1;
    45     LL tmp = dist(a[mid].x[0], a[mid].x[1], p.x[0], p.x[1]);
    46     if (tmp && tmp < ans) ans = tmp;
    47     if (a[mid].x[flag] < p.x[flag]) {
    48       query(mid+1, r, flag^1, p);
    49       if (ans > sqr(a[mid].x[flag]-p.x[flag])) 
    50     query(l, mid-1, flag^1, p);
    51     }else {
    52       query(l, mid-1, flag^1, p);
    53       if (ans > sqr(a[mid].x[flag]-p.x[flag])) 
    54     query(mid+1, r, flag^1, p);
    55     }
    56   }
    57 }kd;
    58 
    59 void work() {
    60   scanf("%d", &n);
    61   for (int i = 1; i <= n; i++) {
    62     scanf("%lld%lld", &a[i].x[0], &a[i].x[1]);
    63     b[i] = a[i];
    64   }
    65   kd.build(1, n, 0);
    66   for (int i = 1; i <= n; i++) {
    67     ans = 1e18;
    68     kd.query(1, n, 0, b[i]);
    69     printf("%lld
    ", ans);
    70   }
    71 }
    72 int main() {
    73   int t; cin >> t;
    74   while (t--) work();
    75   return 0;
    76 }
  • 相关阅读:
    Android开发:关于WebView
    PHP+JQUEY+AJAX实现分页
    Bootstrap入门(二)栅格
    BootStrap入门教程 (一)
    mac 终端命令
    没有功能需求设计文档?对不起,拒绝开发!
    React 入门教程
    Webpack+React+ES6开发模式入门指南
    webpack2.0+ vue2.0
    一个小时学会jQuery
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7895069.html
Copyright © 2011-2022 走看看