zoukankan      html  css  js  c++  java
  • In case of failure

    In case of failure

    http://acm.hdu.edu.cn/showproblem.php?pid=2966

    Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2843    Accepted Submission(s): 1209


    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
     
    Source

    注意,最近点不能是自己,所以需要判cur.first!=0时才加入队列中

     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cstdio>
     6 #define N 100005
     7 using namespace std;
     8 
     9 int n,m,id;//n是点数,m是维度,id是当前切的维度
    10 
    11 struct sair{
    12     long long p[5];
    13     bool operator<(const sair &b)const{
    14         return p[id]<b.p[id];
    15     }
    16 }_data[N],data[N<<3],tt[N];
    17 int flag[N<<3];
    18 
    19 priority_queue<pair<long long,sair> >Q;
    20 
    21 void build(int l,int r,int rt,int dep){
    22     if(l>r) return;
    23     flag[rt]=1;
    24     flag[rt<<1]=flag[rt<<1|1]=-1;
    25     id=dep%m;
    26     int mid=l+r>>1;
    27     nth_element(_data+l,_data+mid,_data+r+1);
    28     data[rt]=_data[mid];
    29     build(l,mid-1,rt<<1,dep+1);
    30     build(mid+1,r,rt<<1|1,dep+1);
    31 }
    32 
    33 void query(sair p,int k,int rt,int dep){
    34     if(flag[rt]==-1) return;
    35     pair<long long,sair> cur(0,data[rt]);//获得当前节点
    36     for(int i=0;i<m;i++){//计算当前节点到P点的距离
    37         cur.first+=(cur.second.p[i]-p.p[i])*(cur.second.p[i]-p.p[i]);
    38     }
    39     int idx=dep%m;
    40     int fg=0;
    41     int x=rt<<1;
    42     int y=rt<<1|1;
    43     if(p.p[idx]>=data[rt].p[idx]) swap(x,y);
    44     if(~flag[x]) query(p,k,x,dep+1);
    45     //开始回溯
    46     if(Q.size()<k){
    47         if(cur.first!=0){
    48             Q.push(cur);
    49         }
    50         fg=1;
    51     }
    52     else{
    53         if(cur.first<Q.top().first){
    54             if(cur.first!=0){
    55                 Q.pop();
    56                 Q.push(cur);
    57             }
    58         }
    59         if((p.p[idx]-data[rt].p[idx])*(p.p[idx]-data[rt].p[idx])<Q.top().first){
    60             fg=1;
    61         }
    62     }
    63     if(~flag[y]&&fg){
    64         query(p,k,y,dep+1);
    65     }
    66 }
    67 
    68 long long ans;
    69 
    70 
    71 int main(){
    72     int T;
    73     scanf("%d",&T);
    74     while(T--){
    75         m=2;
    76         scanf("%d",&n);
    77         for(int i=1;i<=n;i++){
    78             for(int j=0;j<m;j++){
    79                 scanf("%lld",&_data[i].p[j]);
    80                 tt[i].p[j]=_data[i].p[j];
    81             }
    82         }
    83         build(1,n,1,0);
    84         int k=1;
    85         for(int i=1;i<=n;i++){
    86             sair tmp;
    87             for(int j=0;j<m;j++)
    88                 tmp.p[j]=tt[i].p[j];
    89             while(!Q.empty()){
    90                 Q.pop();
    91             }
    92             query(tmp,k,1,0);
    93             ans=Q.top().first;
    94             Q.pop();
    95             printf("%lld
    ",ans);
    96         }
    97     }
    98 }
    View Code
  • 相关阅读:
    tcp socket
    Spring MVC 复习笔记01
    Spring 复习笔记01
    mybatis 复习笔记03
    ActiveMQ实现负载均衡+高可用部署方案(转)
    JVM复习笔记
    JAVA NIO复习笔记
    java IO复习笔记
    并发编程复习笔记
    杂记复习笔记
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9873378.html
Copyright © 2011-2022 走看看