#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define sz(x) (int)x.size()
#define f first
#define s second
/*
Date: 2022/06/10
Problem Link: https://oj.uz/problem/view/JOI21_road_construction
Topic(s): Binary search, sorting
Time Spent: 1 hr impl
Solution Outline:
- The distance between two points can be written as max(abs((xi + yi) - (xj + yj)), abs((xi - yi) - (xj - yj)))
Binary search on v, which is the kth largest value. We have to check whether there more or less than k roads with
cost <= v.
- Maintain a sliding window of size v.
- Sort the points by xi + yi. For a point i, remove all points j that has xj + yj further than v units away.
- Put xi - yi into the set.
- We know all js that came before i must have xi + yi - (xj + yj) <= v. So we can simply call lower_bound on the set
with xi - yi - v and vi - vi + v. The range that it gives us will be the amount of points within v distance of i.
*/
/*
AC at 2022/06/10 14:14
*/
const int MAXN = 2e5+5;
int n, k;
vector<pll> arr;
vector<ll> res;
ll sum(const pll& a){
return a.f + a.s;
}
ll dif(const pll& a){
return a.f - a.s;
}
bool check(ll v, bool upd){
int l = 0;
auto cmp = [](const pll& a, const pll& b){
if (dif(a) == dif(b)){
return a < b;
}else{
return a.f - a.s < b.f - b.s;
}
};
set<pll, decltype(cmp)> set(cmp);
ll ans = 0;
for (int i=0; i<n; i++){
while (l < n && sum(arr[i]) - sum(arr[l]) > v){
set.erase(arr[l++]);
}
auto inc = set.lower_bound(arr[i]);
auto dec = inc;
while (inc != set.end() && dif(*inc) - dif(arr[i]) <= v){
// If we are updating the answer, than we have to find all the roads that can be built, bc we only take the k minimum.
if (upd) res.push_back(abs(arr[i].f - (*inc).f) + abs(arr[i].s - (*inc).s));
inc++;
ans++;
// If we are not trying to get the final answer, we just need to know whether there is more than k roads with cost <= v or not.
// We don't care which roads they are.
if (!upd && ans >=k){
return true;
}
}
while (dec != set.begin() && dif(arr[i]) - dif(*prev(dec)) <= v){
dec--;
if (upd) res.push_back(abs(arr[i].f - (*dec).f) + abs(arr[i].s - (*dec).s));
ans++;
if (!upd && ans >=k){
return true;
}
}
set.insert(arr[i]);
}
return ans >= k;
}
int main(){
cin.tie(0); ios_base::sync_with_stdio(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
cin >> n >> k;
arr.resize(n);
for (int i=0; i<n; i++){
int x, y;
cin >> x >> y;
arr[i] = {x, y};
}
sort(arr.begin(), arr.end(), [](const pll& a, const pll& b){
return a.f + a.s < b.f + b.s;
});
ll low = 0, high = 4e9;
while (low < high){
ll mid = (low + high)/2;
if (check(mid, false)){
high = mid;
}else{
low = mid+1;
}
}
check(low, true);
assert((int)res.size() >= k);
sort(res.begin(), res.end());
for (int i=0; i<k; i++){
cout << res[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
4920 KB |
Output is correct |
2 |
Correct |
135 ms |
5084 KB |
Output is correct |
3 |
Correct |
123 ms |
5072 KB |
Output is correct |
4 |
Correct |
109 ms |
5084 KB |
Output is correct |
5 |
Correct |
120 ms |
3896 KB |
Output is correct |
6 |
Correct |
6 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
7540 KB |
Output is correct |
2 |
Correct |
366 ms |
7476 KB |
Output is correct |
3 |
Correct |
108 ms |
4980 KB |
Output is correct |
4 |
Correct |
292 ms |
8388 KB |
Output is correct |
5 |
Correct |
309 ms |
9624 KB |
Output is correct |
6 |
Correct |
299 ms |
9532 KB |
Output is correct |
7 |
Correct |
269 ms |
8512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
4180 KB |
Output is correct |
2 |
Correct |
307 ms |
4180 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
139 ms |
6344 KB |
Output is correct |
5 |
Correct |
693 ms |
8492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
4180 KB |
Output is correct |
2 |
Correct |
307 ms |
4180 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
139 ms |
6344 KB |
Output is correct |
5 |
Correct |
693 ms |
8492 KB |
Output is correct |
6 |
Correct |
448 ms |
4180 KB |
Output is correct |
7 |
Correct |
416 ms |
4180 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
303 ms |
4180 KB |
Output is correct |
11 |
Correct |
108 ms |
6396 KB |
Output is correct |
12 |
Correct |
680 ms |
4248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
4920 KB |
Output is correct |
2 |
Correct |
135 ms |
5084 KB |
Output is correct |
3 |
Correct |
123 ms |
5072 KB |
Output is correct |
4 |
Correct |
109 ms |
5084 KB |
Output is correct |
5 |
Correct |
120 ms |
3896 KB |
Output is correct |
6 |
Correct |
6 ms |
340 KB |
Output is correct |
7 |
Correct |
959 ms |
6020 KB |
Output is correct |
8 |
Correct |
960 ms |
6080 KB |
Output is correct |
9 |
Correct |
110 ms |
5052 KB |
Output is correct |
10 |
Correct |
510 ms |
5380 KB |
Output is correct |
11 |
Correct |
306 ms |
5180 KB |
Output is correct |
12 |
Correct |
651 ms |
7284 KB |
Output is correct |
13 |
Correct |
605 ms |
6184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
4920 KB |
Output is correct |
2 |
Correct |
135 ms |
5084 KB |
Output is correct |
3 |
Correct |
123 ms |
5072 KB |
Output is correct |
4 |
Correct |
109 ms |
5084 KB |
Output is correct |
5 |
Correct |
120 ms |
3896 KB |
Output is correct |
6 |
Correct |
6 ms |
340 KB |
Output is correct |
7 |
Correct |
366 ms |
7540 KB |
Output is correct |
8 |
Correct |
366 ms |
7476 KB |
Output is correct |
9 |
Correct |
108 ms |
4980 KB |
Output is correct |
10 |
Correct |
292 ms |
8388 KB |
Output is correct |
11 |
Correct |
309 ms |
9624 KB |
Output is correct |
12 |
Correct |
299 ms |
9532 KB |
Output is correct |
13 |
Correct |
269 ms |
8512 KB |
Output is correct |
14 |
Correct |
216 ms |
4180 KB |
Output is correct |
15 |
Correct |
307 ms |
4180 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
139 ms |
6344 KB |
Output is correct |
18 |
Correct |
693 ms |
8492 KB |
Output is correct |
19 |
Correct |
448 ms |
4180 KB |
Output is correct |
20 |
Correct |
416 ms |
4180 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
303 ms |
4180 KB |
Output is correct |
24 |
Correct |
108 ms |
6396 KB |
Output is correct |
25 |
Correct |
680 ms |
4248 KB |
Output is correct |
26 |
Correct |
959 ms |
6020 KB |
Output is correct |
27 |
Correct |
960 ms |
6080 KB |
Output is correct |
28 |
Correct |
110 ms |
5052 KB |
Output is correct |
29 |
Correct |
510 ms |
5380 KB |
Output is correct |
30 |
Correct |
306 ms |
5180 KB |
Output is correct |
31 |
Correct |
651 ms |
7284 KB |
Output is correct |
32 |
Correct |
605 ms |
6184 KB |
Output is correct |
33 |
Correct |
2013 ms |
8312 KB |
Output is correct |
34 |
Correct |
1974 ms |
8272 KB |
Output is correct |
35 |
Correct |
1262 ms |
7648 KB |
Output is correct |
36 |
Correct |
1158 ms |
14128 KB |
Output is correct |
37 |
Correct |
1186 ms |
14364 KB |
Output is correct |
38 |
Correct |
1018 ms |
8576 KB |
Output is correct |