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;
using ll = long long;
#define dbg(x) cerr << #x << ": " << x << endl;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k, n;
cin >> k >> n;
ll ans = 0;
vector<int> coords;
vector<pair<int, int>> pairs;
for (int i = 0; i < n; ++i) {
char c1;
cin >> c1;
int x1;
cin >> x1;
char c2;
cin >> c2;
int x2;
cin >> x2;
if (c1 == c2) {
ans += abs(x1 - x2);
} else {
coords.push_back(x1);
coords.push_back(x2);
pairs.emplace_back(x1, x2);
++ans;
}
}
sort(coords.begin(), coords.end());
int m = coords.size();
if (k == 1) {
for (auto x : coords) {
ans += abs(x - coords[m / 2]);
}
} else {
const ll INF = 1e17;
ll mn = INF;
for (int i = 0; i < m; ++i) {
for (int j = i + 1; j < m; ++j) {
ll curr_ans = 0;
for (const auto& [x1, x2] : pairs) {
curr_ans += min(abs(x1 - coords[i]) + abs(x2 - coords[i]), abs(x1 - coords[j]) + abs(x2 - coords[j]));
}
mn = min(mn, curr_ans);
}
}
ans += mn;
}
cout << ans << '\n';
return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# | 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... |