【HDOJ】4347 The Closest M Points
居然是KD解。
/* 4347 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#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 clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 int id, n, m, K; typedef struct node_t {
int x[]; friend bool operator< (const node_t& a, const node_t& b) {
return a.x[id] < b.x[id];
} double Distance(const node_t& a) {
__int64 ret = ; rep(i, , K)
ret += 1LL*(a.x[i]-x[i])*(a.x[i]-x[i]); return ret;
} void print() {
printf("%d", x[]);
rep(i, , K)
printf(" %d", x[i]);
putchar('\n');
} } node_t; typedef struct Node {
__int64 d;
node_t p; Node() {}
Node(__int64 d, node_t& p):
d(d), p(p) {} friend bool operator< (const Node& a, const Node& b) {
return a.d < b.d;
} } Node; const int maxn = ;
node_t nd[maxn];
vector<node_t> ans; typedef struct KD_tree{
static const int maxd = ;
static const int maxn = ;
node_t P[maxn<<];
int idx[maxn<<];
priority_queue<Node> Q; void Build(int deep, int l, int r, int rt) {
if (l > r)
return ;
idx[rt] = deep % K;
if (l == r) {
P[rt] = nd[l];
return ;
} id = idx[rt];
int mid = (l + r) >> ;
nth_element(nd+l, nd+mid, nd+r+);
P[rt] = nd[mid];
Build(deep+, l, mid-, rt<<);
Build(deep+, mid+, r, rt<<|);
} void Query(node_t x, int l, int r, int rt) {
if (l > r)
return ; int id = idx[rt];
__int64 tmp = x.Distance(P[rt]);
if (l == r) {
if (SZ(Q) < m) {
Q.push(Node(tmp, P[rt]));
} else {
if (tmp < Q.top().d) {
Q.pop();
Q.push(Node(tmp, P[rt]));
}
}
return ;
} int mid = (l + r) >> ; if (P[rt].x[id] >= x.x[id]) {
Query(x, l, mid-, rt<<);
if (SZ(Q) < m) {
Q.push(Node(tmp, P[rt]));
Query(x, mid+, r, rt<<|);
} else {
if (tmp < Q.top().d) {
Q.pop();
Q.push(Node(tmp, P[rt]));
}
if (1LL*(x.x[id]-P[rt].x[id])*(x.x[id]-P[rt].x[id]) < Q.top().d) {
Query(x, mid+, r, rt<<|);
}
}
} else {
Query(x, mid+, r, rt<<|);
if (SZ(Q) < m) {
Q.push(Node(tmp, P[rt]));
Query(x, l, mid-, rt<<);
} else {
if (tmp < Q.top().d) {
Q.pop();
Q.push(Node(tmp, P[rt]));
}
if (1LL*(x.x[id]-P[rt].x[id])*(x.x[id]-P[rt].x[id]) < Q.top().d) {
Query(x, l, mid-, rt<<);
}
}
}
} void Dump() {
ans.clr();
while (!Q.empty()) {
ans.pb(Q.top().p);
Q.pop();
}
}
} KD_tree; KD_tree kd; void solve() {
int q, sz;
node_t p; kd.Build(, , n-, );
scanf("%d", &q);
while (q--) {
rep(j, , K)
scanf("%d", &p.x[j]);
scanf("%d", &m);
kd.Query(p, , n-, );
kd.Dump();
sz = SZ(ans);
printf("the closest %d points are:\n", m);
per(i, , sz) {
ans[i].print();
}
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d %d", &n, &K)!=EOF) {
rep(i, , n)
rep(j, , K)
scanf("%d", &nd[i].x[j]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
数据发生器。
from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 10
bound = 10**5
for tt in xrange(t):
n = randint(100, 200)
k = randint(1, 5)
fout.write("%d %d\n" % (n, k))
for i in xrange(n):
L = []
for j in xrange(k):
x = randint(-1000, 1000)
L.append(x)
fout.write(" ".join(map(str, L)) + "\n")
q = randint(20, 30)
fout.write("%d\n" % (q))
for qq in xrange(q):
L = []
for j in xrange(k):
x = randint(-1000, 1000)
L.append(x)
fout.write(" ".join(map(str, L)) + "\n")
m = randint(1, 10)
fout.write("%d\n" % (m)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
【HDOJ】4347 The Closest M Points的更多相关文章
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- 【kd-tree】bzoj3053 The Closest M Points
同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- bzoj 3053 HDU 4347 : The Closest M Points kd树
bzoj 3053 HDU 4347 : The Closest M Points kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- hdu 4347 The Closest M Points (kd树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出 . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说, ...
- hdu 4347 The Closest M Points(KD树)
Problem - 4347 一道KNN的题.直接用kd树加上一个暴力更新就撸过去了.写的时候有一个错误就是搜索一边子树的时候返回有当前层数会被改变了,然后就直接判断搜索另一边子树,搞到wa了半天. ...
- 数据结构(KD树):HDU 4347 The Closest M Points
The Closest M Points Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Ot ...
- HDU 4347 The Closest M Points (kdTree)
赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...
- hud 4347 The Closest M Points(KD-Tree)
传送门 解题思路 \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割.它的插入删除复杂度为\(log^2 n\),它查 ...
随机推荐
- Eclipse经验总结
1.在Project Explorer中显示src.resource目录:通过Project->properties->java Build Path->Source设置 2.解决J ...
- Div 内部所有元素 全部垂直对齐
http://stackoverflow.com/questions/7273338/how-to-vertically-align-an-image-inside-div How it works: ...
- phpcms v9修改栏目描述的多行文本为编辑器方法
phpcms v9在添加栏目的时候,栏目描述为多行文本,无法满足有图片,以及格式的修改调整,那么仿站网今天告诉大家如何将他改为编辑器,方法如下 找到phpcms/moudles/admin/templ ...
- Python本地化例子 - gettext 模块
关键字:Python 3.4,gettext,本地化,Localization OS:Windows 7,Mac 1. 创建一个locsample.py文件,文件内容如下,把所有需要本地化的字符串放到 ...
- php5函数库
* APC缓存 apc_add — 缓存一个变量到数据存储 * DateTime DateTime::addDateTime::diffDateTime::formatDateTime::modify ...
- Android中获取应用程序(包)的信息-----PackageManager的使用(一)
本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagenam ...
- 安装Ubuntu时,遇到自定义交换空间swap大小设置问题
【整理】Ubuntu自定义分区设置 在安装Ubuntu时,如果使用的是一个新硬盘那么安装向导会建议你使用整个硬盘,如果硬盘上已经有数据了,向导会建议使用剩余的空间。不管怎样,是由向导自动划分的分区。 ...
- Oracle表连接
一个普通的语句select * from t1, t2 where t1.id = t2.id and t1.name = 'a'; 这个语句在什么情况下最高效? 表连接分类: 1. 嵌套循环连接(N ...
- log4net 配置
1.在项目中引入log4net.dll组件: 2.在App.congfig中做如下修改 在加入如下内容: 这个节点最好放在<configuration>下的第一个位置,不然在服务里会报错. ...
- c# DirectoryInfo类 详解
DirectoryInfo类和Directory类之间的关系与FileInfo类和File类之间的关系十分类似.下面介绍一下DirectoryInfo类的常用属性. DirectoryInfo类的常用 ...