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);
long long k, n;
cin >> k >> n;
if (k == 1) {
long long same = 0;
vector<int> pa;
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 {
pa.push_back(p);
pa.push_back(pp);
}
}
if (!pa.empty()) {
sort(pa.begin(), pa.end());
int loc = pa[pa.size() / 2];
for (int i = 0; i < (int) pa.size(); i++) {
same += abs(pa[i] - loc);
}
same += (pa.size() / 2);
}
cout << same << '\n';
return 0;
}
long long same = 0;
vector<pair<long long, long long>> pa;
set<long long> se;
for (long long i = 0; i < n; i++) {
char z, zz;
long long 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<long long> P(se.begin(), se.end());
n = pa.size();
long long nn = P.size();
const long long inf = (long long) 8e18L;
long long res = inf;
for (long long k1 = 0; k1 < nn; k1++) {
for (long long k2 = k1; k2 < nn; k2++) {
long long ans = 0;
for (long long j = 0; j < n; j++) {
long long 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... |