zoukankan      html  css  js  c++  java
  • hdu2966 In case of failure(这道题就和ta的name一样,failure)

    Problem 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

    分析:
    KDtree

    tip

    下方高能
    我写好了之后,交上去就是WA

    于是就从网上扒了一个AC代码对拍

    狂拍不止,结果如下
    这里写图片描述

    疯了吗,没错就是WA,我放弃了。。。

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    
    using namespace std;
    
    const ll INF=0x33333333;
    struct node{
        int l,r,d[2],mn[2],mx[2],id;
    };
    node t[100010];
    int root,n,x,y,cmpd,po[100010][2];
    ll ans;
    
    int cmp(const node &a,const node &b)
    {
        return ((a.d[cmpd]<b.d[cmpd])||((a.d[cmpd]==b.d[cmpd])&&(a.d[!cmpd]<b.d[!cmpd])));
    }
    
    void update(int bh)
    {
        int lc=t[bh].l;
        int rc=t[bh].r;
        if (lc)
        {
            t[bh].mn[0]=min(t[bh].mn[0],t[lc].mn[0]);
            t[bh].mn[1]=min(t[bh].mn[1],t[lc].mn[1]);
            t[bh].mx[0]=max(t[bh].mx[0],t[lc].mx[0]);
            t[bh].mx[1]=max(t[bh].mx[1],t[lc].mx[1]);
        }
        if (rc)
        {
            t[bh].mn[0]=min(t[bh].mn[0],t[rc].mn[0]);
            t[bh].mn[1]=min(t[bh].mn[1],t[rc].mn[1]);
            t[bh].mx[0]=max(t[bh].mx[0],t[rc].mx[0]);
            t[bh].mx[1]=max(t[bh].mx[1],t[rc].mx[1]);
        }
    }
    
    int build(int l,int r,int D)
    {
        cmpd=D;
        int mid=(l+r)>>1;
        nth_element(t+l+1,t+mid+1,t+r+1,cmp);
        t[mid].mn[0]=t[mid].mx[0]=t[mid].d[0];
        t[mid].mn[1]=t[mid].mx[1]=t[mid].d[1];
        if (l!=mid) t[mid].l=build(l,mid-1,!D);
        if (r!=mid) t[mid].r=build(mid+1,r,!D);
        update(mid);
        return mid;
    }
    
    ll sqr(ll x) {return x*x;}
    ll minn(ll a,ll b){if (a<b) return a;else return b;}
    
    ll dis(int p,int x,int y)
    {
        ll d=0;
        if (x<t[p].mn[0]) d+=sqr((ll)t[p].mn[0]-x);
        if (x>t[p].mx[0]) d+=sqr((ll)x-t[p].mx[0]);
        if (y<t[p].mn[1]) d+=sqr((ll)t[p].mn[1]-y);
        if (y>t[p].mx[1]) d+=sqr((ll)y-t[p].mx[1]);
        return d;
    }
    
    void ask(int now)
    {
        ll d0,dl,dr;
        d0=sqr((ll)t[now].d[0]-x)+sqr((ll)t[now].d[1]-y);
        if (t[now].l) dl=dis(t[now].l,x,y);
        else dl=INF;
        if (t[now].r) dr=dis(t[now].r,x,y);
        else dr=INF;
        if (d0) ans=minn(ans,d0);
        if (dl<dr)
        {
            if (dl<ans) ask(t[now].l);
            if (dr<ans) ask(t[now].r);
        }
        else
        {
            if (dr<ans) ask(t[now].r);
            if (dl<ans) ask(t[now].l);
        }
    }
    
    ll an[150000];
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            memset(t,0,sizeof(t));
            scanf("%d",&n);
            for (int i=1;i<=n;i++)
                scanf("%d%d",&t[i].d[0],&t[i].d[1]),t[i].id=i;
            root=build(1,n,0);
            for (int i=1;i<=n;i++) 
            {
                x=t[i].d[0]; y=t[i].d[1];
                ans=INF;
                ask(root);
                an[t[i].id]=ans;
            }
            for (int i=1;i<=n;i++) printf("%lld
    ",an[i]); 
        }
        return 0;
    }
  • 相关阅读:
    移动端轮播图实现
    iterator
    Promise对象和运算符
    xshell报错:The remote SSH server rejected X11 forwarding request. Last login: Fri Dec 2
    linux平台运行jmeter
    linux基础命令
    APP登录之后会将PC的登录信息踢掉
    测试中遇到支付的一个小问题
    谷歌浏览器css不支持12px以下的
    spring setter注入
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673344.html
Copyright © 2011-2022 走看看