HDU 4347 The Closest M Points
題目:點選開啟連結
題意:求取一個點的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;
}
相關文章
- 973. K Closest Points to Origin(Leetcode每日一題-2020.11.09)LeetCode每日一題
- Grid Points
- 564-Find the Closest Palindrome
- Machine Learning - Basic pointsMac
- [LeetCode] 681. Next Closest TimeLeetCode
- Leetcode 16 3Sum ClosestLeetCode
- OpenGL Shader Key Points (3)
- POJ - 3090 Visible Lattice Points
- Inspection Points: Key settings and Usage
- 149-Max Points on a Line
- Objects as Points 論文總結Object
- leetcode-149-Max Points on a LineLeetCode
- hdu 2111 Saving HDU (DP)
- P4423 [BJWC2011] 最小三角形 與 SP7209 CLOSEST - Closest Triplet
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- LeetCode 3Sum Closest(016)解法總結LeetCode
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- [LeetCode] 3096. Minimum Levels to Gain More PointsLeetCodeAI
- guarantee restore points-Flashback after RMAN restoreREST
- PAT A1048 二分/two points
- Shape of HDU
- HDU 3349
- HDU 2052(C語言+註釋)+HDU 2090C語言
- 564. Find the Closest Palindrome, 2468. Split Message Based on LimitMIT
- 什麼是 Angular library 的 secondary entry points?Angular
- halcon change_radial_distortion_points運算元
- [譯] 使用 closest() 函式獲取正確的 DOM 元素函式
- 簡單的揹包問題(入門)HDU2602 HDU2546 HDU1864
- HDU 1240 Asteroids!AST
- hdu2083
- hdu5532
- HDU4787
- hdu5540
- HDU 4921 Map
- HDU 3661 Assignments
- hdu 6415 - DP
- HDU 1729 Stone GameGAM
- HDU 3590 PP and QQ