#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:
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.
*/
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]);
l++;
}
auto inc = set.lower_bound(arr[i]);
auto dec = inc;
while (inc != set.end() && dif(*inc) - dif(arr[i]) <= v){
if (upd) res.push_back(abs(arr[i].f - (*inc).f) + abs(arr[i].s - (*inc).s));
inc++;
ans++;
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 |
130 ms |
4940 KB |
Output is correct |
2 |
Correct |
132 ms |
4948 KB |
Output is correct |
3 |
Correct |
123 ms |
5068 KB |
Output is correct |
4 |
Correct |
112 ms |
5068 KB |
Output is correct |
5 |
Correct |
126 ms |
3808 KB |
Output is correct |
6 |
Correct |
5 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
375 ms |
7576 KB |
Output is correct |
2 |
Correct |
373 ms |
10532 KB |
Output is correct |
3 |
Correct |
113 ms |
5004 KB |
Output is correct |
4 |
Correct |
293 ms |
11536 KB |
Output is correct |
5 |
Correct |
307 ms |
12632 KB |
Output is correct |
6 |
Correct |
310 ms |
12548 KB |
Output is correct |
7 |
Correct |
272 ms |
11600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
4180 KB |
Output is correct |
2 |
Correct |
309 ms |
4180 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
139 ms |
6408 KB |
Output is correct |
5 |
Correct |
693 ms |
8448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
4180 KB |
Output is correct |
2 |
Correct |
309 ms |
4180 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
139 ms |
6408 KB |
Output is correct |
5 |
Correct |
693 ms |
8448 KB |
Output is correct |
6 |
Correct |
457 ms |
4180 KB |
Output is correct |
7 |
Correct |
413 ms |
4180 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
316 ms |
4180 KB |
Output is correct |
11 |
Correct |
110 ms |
6392 KB |
Output is correct |
12 |
Correct |
687 ms |
4220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
4940 KB |
Output is correct |
2 |
Correct |
132 ms |
4948 KB |
Output is correct |
3 |
Correct |
123 ms |
5068 KB |
Output is correct |
4 |
Correct |
112 ms |
5068 KB |
Output is correct |
5 |
Correct |
126 ms |
3808 KB |
Output is correct |
6 |
Correct |
5 ms |
340 KB |
Output is correct |
7 |
Correct |
992 ms |
6032 KB |
Output is correct |
8 |
Correct |
978 ms |
6004 KB |
Output is correct |
9 |
Correct |
113 ms |
5080 KB |
Output is correct |
10 |
Correct |
506 ms |
5248 KB |
Output is correct |
11 |
Correct |
309 ms |
7280 KB |
Output is correct |
12 |
Correct |
662 ms |
9280 KB |
Output is correct |
13 |
Correct |
613 ms |
8288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
4940 KB |
Output is correct |
2 |
Correct |
132 ms |
4948 KB |
Output is correct |
3 |
Correct |
123 ms |
5068 KB |
Output is correct |
4 |
Correct |
112 ms |
5068 KB |
Output is correct |
5 |
Correct |
126 ms |
3808 KB |
Output is correct |
6 |
Correct |
5 ms |
340 KB |
Output is correct |
7 |
Correct |
375 ms |
7576 KB |
Output is correct |
8 |
Correct |
373 ms |
10532 KB |
Output is correct |
9 |
Correct |
113 ms |
5004 KB |
Output is correct |
10 |
Correct |
293 ms |
11536 KB |
Output is correct |
11 |
Correct |
307 ms |
12632 KB |
Output is correct |
12 |
Correct |
310 ms |
12548 KB |
Output is correct |
13 |
Correct |
272 ms |
11600 KB |
Output is correct |
14 |
Correct |
215 ms |
4180 KB |
Output is correct |
15 |
Correct |
309 ms |
4180 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
139 ms |
6408 KB |
Output is correct |
18 |
Correct |
693 ms |
8448 KB |
Output is correct |
19 |
Correct |
457 ms |
4180 KB |
Output is correct |
20 |
Correct |
413 ms |
4180 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
316 ms |
4180 KB |
Output is correct |
24 |
Correct |
110 ms |
6392 KB |
Output is correct |
25 |
Correct |
687 ms |
4220 KB |
Output is correct |
26 |
Correct |
992 ms |
6032 KB |
Output is correct |
27 |
Correct |
978 ms |
6004 KB |
Output is correct |
28 |
Correct |
113 ms |
5080 KB |
Output is correct |
29 |
Correct |
506 ms |
5248 KB |
Output is correct |
30 |
Correct |
309 ms |
7280 KB |
Output is correct |
31 |
Correct |
662 ms |
9280 KB |
Output is correct |
32 |
Correct |
613 ms |
8288 KB |
Output is correct |
33 |
Correct |
2011 ms |
13448 KB |
Output is correct |
34 |
Correct |
1991 ms |
13444 KB |
Output is correct |
35 |
Correct |
1288 ms |
12708 KB |
Output is correct |
36 |
Correct |
1160 ms |
19508 KB |
Output is correct |
37 |
Correct |
1184 ms |
19384 KB |
Output is correct |
38 |
Correct |
1043 ms |
13836 KB |
Output is correct |