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;
typedef long long ll;
typedef pair<int, int> ii;
vector<ii> v;
ll get(ll x) {
ll ret = 0;
for(ii a : v) ret += abs(a.first - x) + abs(a.second - x);
return ret;
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
int k, n; cin >> k >> n;
ll pre = 0;
vector<int> pos;
for(int i = 0; i < n; i++) {
char p, q; int s, t;
cin >> p >> s >> q >> t;
if(p == q) pre += abs(s - t);
else v.push_back({s, t}), pos.push_back(s), pos.push_back(t);
}
int m = pos.size();
sort(pos.begin(), pos.end());
ll mn = get(pos[m/2]);
if(m/2+1 < m) mn = min(mn, get(pos[m/2 + 1]));
cout << mn + v.size() + pre << endl;
}
# | 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... |