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 s = 0;
vector<pair<int, int>> pairs;
vector<int> coords;
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) {
s += abs(x1 - x2);
} else {
if (x1 > x2) swap(x1, x2);
pairs.emplace_back(x1, x2);
coords.push_back(x1);
coords.push_back(x2);
}
}
sort(coords.begin(), coords.end());
coords.resize(unique(coords.begin(), coords.end()) - coords.begin());
s += pairs.size();
const ll INF = 1e17;
ll ans = INF;
for (auto d : coords) {
ll curr_ans = 0;
for (const auto& [x1, x2] : pairs) {
curr_ans += abs(x1 - d) + abs(x2 - d);
}
ans = min(ans, curr_ans + s);
}
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
*/
# | 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... |