This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
int K, N;
cin >> K >> N;
vector<int> P(N), Q(N);
vector<int> A, B;
long long ans = 0, tot = LLONG_MAX;
vector<int> D;
for (int i = 0; i < N; i++) {
char X, Y;
cin >> X >> P[i] >> Y >> Q[i];
if (X > Y) {
swap(X, Y);
}
if (X == Y) {
ans += abs(P[i] - Q[i]);
} else {
D.push_back(Q[i]);
A.push_back(P[i]);
B.push_back(Q[i]);
}
}
P = A, Q = B;
N = (int)P.size();
if (K == 1) {
for (int x : D) {
long long res = 0;
for (int i = 0; i < N; i++) {
res += abs(P[i] - x) + abs(Q[i] - x) + 1;
}
tot = min(tot, ans + res);
}
} else {
for (int x : D) {
for (int y : D) {
long long res = 0;
for (int i = 0; i < N; i++) {
res += min(abs(P[i] - x) + abs(Q[i] - x), abs(P[i] - y) + abs(Q[i] - y)) + 1;
}
tot = min(tot, ans + res);
}
}
}
cout << tot << "\n";
}
# | 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... |