Submission #1124637

#TimeUsernameProblemLanguageResultExecution timeMemory
1124637votranngocvyRoad Construction (JOI21_road_construction)C++20
100 / 100
1621 ms10404 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int,int> #define fi first #define se second const int N = 25e4 + 5; pii a[N]; priority_queue<int>q; int n,k; int dist(int i,int j) { return max(abs(a[i].fi - a[j].fi),abs(a[i].se - a[j].se)); } bool check(int mid) { set<pii>s; int j = 1,cnt = 0; for (int i = 1; i <= n; i++) { while (abs(a[i].fi - a[j].fi) > mid) { s.erase({a[j].se,j}); j++; } for (auto it = s.lower_bound({a[i].se - mid,-1}); it != s.end(); it++) { if (abs((*it).fi - a[i].se) > mid) break; cnt++; if (cnt >= k) return true; } s.insert({a[i].se,i}); } return false; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i].fi >> a[i].se; int x = a[i].fi,y = a[i].se; a[i] = {x + y,x - y}; } sort(a + 1,a + n + 1); int l = 0,r = 4e9,ans = 4e9; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) ans = mid,r = mid - 1; else l = mid + 1; } set<pii>s; while (!q.empty()) q.pop(); int j = 1; for (int i = 1; i <= n; i++) { while (abs(a[i].fi - a[j].fi) > ans) { s.erase({a[j].se,j}); j++; } for (auto it = s.lower_bound({a[i].se - ans,-1}); it != s.end(); it++) { if (abs((*it).fi - a[i].se) > ans) break; q.push(dist(i,(*it).se)); if (q.size() > k) q.pop(); } s.insert({a[i].se,i}); } vector<int>vec; while (!q.empty()) { vec.push_back(q.top()); q.pop(); } sort(vec.begin(),vec.end()); for (auto x: vec) cout << x << "\n"; } //Hint: xoay bảng 45 độ để chuyển khoảng cách Mahattan thành khoảng cách Cherbyshev //(x,y) -> (x + y,x - y)
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...