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;
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<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) 4e18L;
long long res = inf;
for (int k1 = 0; k1 < nn; k1++) {
for (int k2 = k1; k2 < nn; k2++) {
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += 1 + min(abs(P[k1] - pa[i].first) + abs(P[k1] - pa[i].second), abs(P[k2] - pa[i].first) + abs(P[k2] - pa[i].second));
}
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... |