题意
给定 \(n\) 个点,求平面上,曼哈顿距离最近的 \(k\) 点对。
Sol
仔细想想就会发现,曼哈顿距离不好做最近 \(k\) 点对。
考虑转成切比雪夫距离。\(x' = x + y, y' = x - y\)。
二分答案,每次 \(check\) 一个 \(dis\),询问距离小于 \(dis\) 的点对是否有 \(k\) 个。
\(check\) 是平凡的,用尺取做掉一维数据结构维护另一维即可。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <set>
#include <cassert>
#define int long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
#define fi first
#define se second
const int N = 2.5e5 + 5, inf = 1e10;
set <pii> isl;
array <pii, N> s;
array <int, N> cur;
int cnt;
bool check(int x, int n, int k) {
int rd = 1;
isl.clear(); cnt = 0;
for (int i = 1; i <= n; i++) {
rd = max(rd, i);
isl.erase(make_pair(s[i].se, s[i].fi));
while (rd < n && s[rd + 1].fi - s[i].fi <= x) {
rd++;
isl.insert(make_pair(s[rd].se, s[rd].fi));
}
auto it = isl.lower_bound(make_pair(s[i].se - x, -inf));
/* if (i == 1 && it != isl.end()) { */
/* write(s[i].fi), putchar(32); */
/* write(s[i].se), puts(""); */
/* } */
for (; it != isl.end(); it++) {
int tp = max(abs(it -> se - s[i].fi), abs(it -> fi - s[i].se));
if (tp > x) break;
cnt++, cur[cnt] = tp;
if (cnt >= k) return true;
}
}
return false;
}
signed main() {
int n = read(), k = read();
for (int i = 1; i <= n; i++) {
int x = read(), y = read();
s[i] = make_pair(x + y, x - y);
}
sort(s.begin() + 1, s.begin() + 1 + n);
int l = 1, r = 1e10 + 5;
int ans = -1;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid, n, k)) ans = r = mid;
else l = mid + 1;
}
check(ans - 1, n, k);
sort(cur.begin() + 1, cur.begin() + 1 + cnt);
for (int i = 1; i <= cnt; i++)
write(cur[i]), puts("");
for (int i = cnt + 1; i <= k; i++)
write(ans), puts("");
return 0;
}
标签:建設案,cur,int,Day2,cnt,JOISC,include,check,define
From: https://www.cnblogs.com/cxqghzj/p/17861294.html