QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#139369 | #4423. AMPPZ in the times of disease | ckiseki | TL | 0ms | 0kb | C++20 | 2.1kb | 2023-08-13 05:15:26 | 2023-08-13 05:15:28 |
answer
#include <bits/stdc++.h>
using namespace std;
class DSU {
vector<int> a;
public:
DSU(int n) : a(n) {
iota(a.begin(), a.end(), 0);
}
int query(int x) {
if (a[x] != x)
a[x] = query(a[x]);
return a[x];
}
void merge(int x, int y) {
a[query(x)] = query(y);
}
};
using llf = long double;
struct P {
int64_t x, y;
};
llf dis(P a, P b) {
int64_t dx = a.x - b.x;
int64_t dy = a.y - b.y;
return sqrt(llf(dx * dx + dy * dy));
}
pair<int, int> closest(const vector<P> &a) {
struct cmp_y {
bool operator()(const pair<P, int> &p, const pair<P, int> &q) const {
return tie(p.first.y, p.second) < tie(q.first.y, q.second);
}
};
set<pair<P, int>, cmp_y> s;
pair<int, int> ans = {0, 1};
vector<int> o(a.size());
iota(o.begin(), o.end(), 0);
sort(o.begin(), o.end(), [&](int p, int q) {
return tie(a[p].x, a[p].y) < tie(a[q].x, a[q].y);
});
llf d = 1e100; int pt = 0;
for (int i = 0; i < int(a.size()); ++i) {
while (pt < i and a[o[i]].x - a[o[pt]].x >= d) {
s.erase({a[o[pt]], o[pt]});
pt += 1;
}
auto it = s.lower_bound({P(a[o[i]].x, a[o[i]].y - d), -1});
while (it != s.end() and it->first.y - a[o[i]].y < d) {
llf curd = dis(it->first, a[o[i]]);
if (curd < d) {
ans = {it->second, o[i]};
d = curd;
}
it++;
}
s.emplace(a[o[i]], o[i]);
}
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<P> a(n);
for (auto &[x, y] : a)
cin >> x >> y;
DSU dsu(n);
unordered_set<int> s;
for (int i = 0; i < k; ++i)
s.insert(i);
for (int i = k; i < n; ++i) {
vector<int> v;
vector<P> que;
s.insert(i);
for (auto si : s) {
v.push_back(si);
que.push_back(a[si]);
}
auto [x, y] = closest(que);
dsu.merge(v[x], v[y]);
s.erase(v[x]);
s.erase(v[y]);
s.insert(v[x]);
}
vector<int> ans(n);
for (int i = 0; i < n; ++i)
if (dsu.query(i) == i)
ans[i] = k--;
for (int i = 0; i < n; ++i)
cout << ans[dsu.query(i)] << " \n"[i + 1 == n];
}
return 0;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
100 100000 20 270505 510725 245104 76414 131477 992924 781607 895592 562469 622976 564354 637966 980036 112090 522903 687218 113871 977855 6615 123673 13847 347618 657794 165707 420561 183995 11367 136391 507836 694877 985069 105115 774110 486921 14319 338715 774937 118145 981468 99089 803866 491315...
output:
1 16 18 4 6 6 14 10 18 13 8 7 12 13 10 14 17 8 11 14 17 7 3 4 2 1 12 19 3 6 16 7 5 2 19 4 11 12 4 5 5 8 16 1 6 3 1 20 17 20 3 20 7 3 3 15 13 1 13 3 3 11 15 8 4 12 11 19 2 11 5 7 19 2 13 7 11 17 15 9 4 14 18 17 18 8 12 2 20 17 16 6 6 3 3 4 17 2 14 3 17 1 13 1 13 3 13 12 11 17 2 5 3 2 10 20 6 3 10 10 ...