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() {
ios::sync_with_stdio(false);
cin.tie(0);
int k, n;
cin >> k >> n;
long long same = 0;
vector<pair<int, int>> pa;
set<int> se;
for (int i = 0; i < n; i++) {
char z, zz;
int p, pp;
cin >> z >> p >> zz >> pp;
if (z == zz) {
same += abs(p - pp);
} else {
if (z == 'B') {
swap(p, pp);
}
pa.emplace_back(p, pp);
}
se.insert(p);
se.insert(pp);
}
vector<int> P(se.begin(), se.end());
n = pa.size();
int nn = P.size();
const long long inf = (long long) 8e18L;
long long res = inf;
for (int k1 = 0; k1 < nn; k1++) {
for (int k2 = 0; k2 < nn; k2++) {
long long ans = 0;
for (int j = 0; j < n; j++) {
int loc1 = P[k1], loc2 = P[k2];
long long d1 = abs(pa[j].first - loc1) + abs(pa[j].second - loc1);
long long d2 = abs(pa[j].first - loc2) + abs(pa[j].second - loc2);
ans += 1 + min(d1, d2);
}
res = min(res, ans);
}
}
cout << res + same << '\n';
return 0;
}
# | 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... |