2016ACM/ICPC浜氭床鍖洪潚宀涚珯 K Hdu-5992 Finding Hotels KDtree

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016ACM/ICPC浜氭床鍖洪潚宀涚珯 K Hdu-5992 Finding Hotels KDtree相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ret' title='ret'>ret   span   void   ||   point   --   php   icp   tmp   

棰橀潰

棰樻剰:浜岀淮骞抽潰涓婃湁寰堝鐐?姣忎釜鐐规湁涓潈鍊?鐜板湪缁欎綘涓€涓偣(寰堝缁?,鏉冨€紇,璁╀綘鎵惧埌鏉冨€煎皬浜庣瓑浜巚鐨勭偣涓杩欎釜鐐规渶杩戠殑,鐩稿悓鐨勮緭鍑篿d灏忕殑

棰樿В:寰堣8鐨凨Dtree,浣嗘槸鏌ヨ鐨勬椂鍊欐湁2涓皬闄愬埗,

        1涓槸瑕佸皬浜庣瓑浜巚,1涓槸杈撳嚭鏈€灏廼d

         绗竴涓?瀵规瘡涓偣鍒ゆ柇dis鐨勬椂鍊?濡傛灉浠烽挶楂樹簬v 璺濈灏卞彉涓篒NF 浣庝簬v灏辨病鏈夊奖鍝?/p>

         绗簩涓?濡傛灉disl鎴栬€卍isr 鍜屽綋鍓嶆渶杩戠殑dis 鐩哥瓑 灏辩户缁闂互寰楀埌鏇村皬鐨刬d 

 1 #include<bits/stdc++.h>
 2 typedef long long ll;
 3 using namespace std;
 4 const int N=2e5+5;
 5 const ll INF=1e18;
 6 int T,n,m;
 7 struct point 
 8 {
 9     ll x[2];
10     int c,id;
11 }p[N],d;
12 ll idd,ansid,ans;
13 ll sqr(ll x) {return x*x;}
14 int cur;
15 bool cmp(point a,point b) 
16 {
17     return a.x[cur]<b.x[cur];
18 }
19 void build(int l,int r,int dep)
20 {
21     if(l>=r) return;
22     int mid=(l+r)>>1;
23     cur=dep%2;
24     nth_element(p+l,p+mid,p+r+1,cmp);
25     build(l,mid-1,dep+1);
26     build(mid+1,r,dep+1);
27 }
28 ll dis(point a,point b) 
29 {
30     ll ans=0;
31     for(int i=0;i<2;++i) ans+=sqr(a.x[i]-b.x[i]);
32     return ans;
33 }
34 ll ff(point x,int l,int r,int dep) 
35 {
36     int cur=dep%2;
37     if(l>=r) {
38         if(l==r&&x.c>=p[l].c) {
39             ll cnt=dis(x,p[l]);
40             if(p[l].c<=x.c&&(cnt<ans||(cnt==ans&&p[l].id<ansid))) 
41             {
42                 ansid=p[l].id;
43                 ans=cnt;
44                 idd=l;
45             }
46         }
47         return INF;
48     }
49     int mid=(l+r)>>1;
50     ll res=dis(x,p[mid]);
51     if(p[mid].c<=x.c&&(res<ans||(res==ans&&p[mid].id<ansid))) 
52     {
53         ans=res;ansid=p[mid].id;
54         idd=mid;
55     }
56     ll tmp;
57     if(x.x[cur]<p[mid].x[cur]) 
58     {
59         tmp=ff(x,l,mid-1,dep+1);
60         if(ans>sqr(x.x[cur]-p[mid].x[cur])) ff(x,mid+1,r,dep+1);
61     }
62     else 
63     {
64         tmp=ff(x,mid+1,r,dep+1);
65         if(ans>sqr(x.x[cur]-p[mid].x[cur])) ff(x,l,mid-1,dep+1);
66     }
67 }
68 int main() 
69 {
70     scanf("%d",&T);
71     while(T--) 
72     {
73         scanf("%d%d",&n,&m);
74         for(int i=0;i<n;++i) 
75         {
76             scanf("%d%d%d",&p[i].x[0],&p[i].x[1],&p[i].c);
77             p[i].id=i;
78         }
79         build(0,n-1,0);
80         while(m--) 
81         {
82             scanf("%d%d%d",&d.x[0],&d.x[1],&d.c);
83             ans=INF;
84             ff(d,0,n-1,0);
85             printf("%lld %lld %lld
",p[idd].x[0],p[idd].x[1],p[idd].c);
86         }
87     }
88 }

 

以上是关于2016ACM/ICPC浜氭床鍖洪潚宀涚珯 K Hdu-5992 Finding Hotels KDtree的主要内容,如果未能解决你的问题,请参考以下文章

2016 ACM/ICPC Asia Regional Qingdao Online HDU5878

2016 ACM/ICPC Asia Regional Qingdao Online HDU5879

2016 ACM/ICPC Asia Regional Qingdao Online HDU5882

HDU 5976 Detachment 贪心 (2016ACM/ICPC亚洲区大连站)

2016 ACM/ICPC Asia Regional Qingdao Online 1002 Cure

2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873