HDU 4347 The Closest M Points

Self-Discipline發表於2018-09-04

題目:點選開啟連結

題意:求取一個點的m個最近的點

分析:kd樹模板題。入門可參考https://blog.csdn.net/u013534123/article/details/80952174,程式碼細節可參考https://blog.csdn.net/HackerTom/article/details/78198767

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iomanip>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cctype>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define pt(a) cout<<a<<endl
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e5+10;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int n,m,k,t,q,idx,ex[N<<2];
struct nd {
    int v[5];
    bool operator < (const nd &b) const {
        return v[idx]<b.v[idx];
    }
}kdt[N<<2],p[N],aim;

void build(int l,int r,int rt,int dep) {
    ex[rt]=1;
    ex[rt<<1]=ex[rt<<1|1]=0;
    idx=dep%k;
    int m=(l+r)>>1;
    nth_element(p+l,p+m,p+r+1);
    kdt[rt]=p[m];
    if(l<m) build(l,m-1,rt<<1,dep+1);
    if(r>m) build(m+1,r,rt<<1|1,dep+1);
}

typedef pair<int,nd> P;
priority_queue<P> pq;

void qy(int rt,int m,int dep) {
    if(!ex[rt]) return ;
    P now(0,kdt[rt]);
    rep(i,0,k-1) now.fi+=(aim.v[i]-kdt[rt].v[i])*(aim.v[i]-kdt[rt].v[i]);
    int d=dep%k;
    int lc=rt<<1,rc=rt<<1|1;
    if(aim.v[d]>=kdt[rt].v[d]) swap(lc,rc);
    if(ex[lc]) qy(lc,m,dep+1);
    int fd=0;
    if(pq.size()<m) {
        pq.push(now);
        fd=1;
    }else {
        if(now.fi<pq.top().fi) {
            pq.pop();
            pq.push(now);
        }
        if( (aim.v[d]-kdt[rt].v[d])*(aim.v[d]-kdt[rt].v[d]) < pq.top().fi ) fd=1;
    }
    if( ex[rc] && fd ) qy(rc,m,dep+1);
}

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    while(cin>>n>>k) {
        rep(i,0,n-1) rep(j,0,k-1) cin>>p[i].v[j];
        build(0,n-1,1,0);
        cin>>q;
        rep(i,1,q) {
            rep(j,0,k-1) cin>>aim.v[j];
            cin>>m;
            qy(1,m,0);
            int kk=0;
            while(!pq.empty()) p[kk++]=pq.top().se,pq.pop();
            cout<<"the closest "<<m<<" points are:"<<endl;
            for(int x=kk-1;x>=0;x--) {
                for(int y=0;y<k-1;y++) cout<<p[x].v[y]<<" ";
                cout<<p[x].v[k-1]<<endl;
            }
        }
    }
    return 0;
}

 

相關文章