【BZOJ4520】【CQOI2016】K远点对

【题目链接】

【思路要点】

  • KDTree实现K-临近搜索,实现时需要用一个堆来辅助。
  • 时间复杂度\(O(N\sqrt{N}+NKLogK)\)。

代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	100005
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
struct point {long long x, y; };
int cmptype;
bool cmp(point a, point b) {
	if (cmptype) return (a.x < b.x) || (a.x == b.x && a.y < b.y);
	else return (a.y < b.y) || (a.y == b.y && a.x < b.x);
}
point Min(point a, point b) {
	a.x = min(a.x, b.x);
	a.y = min(a.y, b.y);
	return a;
}
point Max(point a, point b) {
	a.x = max(a.x, b.x);
	a.y = max(a.y, b.y);
	return a;
}
struct KD_Tree {
	struct Node {
		point pos;
		point leftup, rightdown;
		int lc, rc;
	};
	Node a[MAXN];
	point p[MAXN], now;
	int n, k, root, size;
	priority_queue <long long, vector<long long>, greater<long long> > Heap;
	void update(int root) {
		a[root].leftup = a[root].rightdown = a[root].pos;
		if (a[root].lc) {
			int tmp = a[root].lc;
			a[root].leftup = Min(a[root].leftup, a[tmp].leftup);
			a[root].rightdown = Max(a[root].rightdown, a[tmp].rightdown);
		}
		if (a[root].rc) {
			int tmp = a[root].rc;
			a[root].leftup = Min(a[root].leftup, a[tmp].leftup);
			a[root].rightdown = Max(a[root].rightdown, a[tmp].rightdown);
		}
	}
	void build(int l, int r, int &root, int type) {
		if (root == 0) root = ++size;
		int mid = (l + r) / 2;
		cmptype = type;
		nth_element(p + l, p + mid, p + r + 1, cmp);
		a[root].pos = p[mid];
		if (mid > l) build(l, mid - 1, a[root].lc, type ^ 1);
		if (mid < r) build(mid + 1, r, a[root].rc, type ^ 1);
		update(root);
	}
	void init() {
		read(n); read(k);
		size = 0;
		for (int i = 1; i <= n; i++)
			read(p[i].x), read(p[i].y);
		build(1, n, root, 0);
	}
	long long dist(point a, point b) {
		return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
	}
	long long dist(point now, int root) {
		long long tmp = max(dist(now, a[root].leftup), dist(now, (point) {a[root].leftup.x, a[root].rightdown.y}));
		long long tnp = max(dist(now, a[root].rightdown), dist(now, (point) {a[root].rightdown.x, a[root].leftup.y}));
		return max(tmp, tnp);
	}
	void work(int root) {
		if (Heap.size() == 2 * k && dist(now, root) <= Heap.top()) return;
		long long tmp = dist(now, a[root].pos);
		Heap.push(tmp);
		if (Heap.size() > 2 * k) Heap.pop();
		if (a[root].lc) work(a[root].lc);
		if (a[root].rc) work(a[root].rc);
	}
	long long calc() {
		for (int i = 1; i <= n; i++) {
			now = p[i];
			work(root);
		}
		return Heap.top();
	}
} KDT;
int main() {
	KDT.init();
	cout << KDT.calc() << endl;
	return 0;
}


BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值