#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n,k;cin >> n >> k;
vector<long long> X(n);
vector<long long> Y(n);
vector<pair<long long,long long>> points(n);
vector<pair<long long,long long>> points2(n);
for(int i = 0;i<n;i++){
cin >> X[i];cin >> Y[i];
points[i].first = X[i];
points[i].second = Y[i];
points2[i].first = Y[i];
points2[i].second = X[i];
}
//obviously I can't construct all possible pairs of roads
//as that will tle
sort(points.begin(),points.end());
sort(points2.begin(),points2.end());
//Subtask 1 : N <= 1000;
//ok grabbed subtask 1
//subtask 3, k = 1
long long lowest = LLONG_MAX;
for(int i = 0;i<n;i++){
long long x = points[i].first;
long long y = points[i].second;
long long n1 = LLONG_MAX;
long long n2 = LLONG_MAX;
if (i == 0){
n2 = abs(x - points[i+1].first) + abs(y-points[i+1].second);
}
else if (i == n-1){
n1 = abs(x - points[i-1].first) + abs(y-points[i-1].second);
}
if (i > 0 && i < n-1){
n1 = abs(x - points[i-1].first) + abs(y-points[i-1].second);
n2 = abs(x - points[i+1].first) + abs(y-points[i+1].second);
}
lowest = min(lowest,n1);
lowest = min(lowest,n2);
}
for(int i = 0;i<n;i++){
long long y = points2[i].first;
long long x = points2[i].second;
long long n1 = LLONG_MAX;
long long n2 = LLONG_MAX;
if (i == 0){
n2 = abs(y - points2[i+1].first) + abs(x-points2[i+1].second);
}
else if (i == n-1){
n1 = abs(y - points2[i-1].first) + abs(x-points2[i-1].second);
}
if (i > 0 && i < n-1){
n1 = abs(y - points2[i-1].first) + abs(x-points2[i-1].second);
n2 = abs(y - points2[i+1].first) + abs(x-points2[i+1].second);
}
lowest = min(lowest,n1);
lowest = min(lowest,n2);
}
cout << lowest;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |