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;
const int N = 100005;
int k, n;
int sz;
long long ans;
pair<int,int> a[N];
vector<int> z;
long long solve1() {
if (n == 0) return 0;
long long res = 0;
int med = z[z.size() / 2];
for (int i = 0; i < z.size(); ++i) {
res += abs(med - z[i]);
}
return res;
}
long long calc(int x, int y) {
long long res = 0;
for (int i = 1; i <= n; ++i) {
int dx = abs(x - a[i].first) + abs(x - a[i].second);
int dy = abs(y - a[i].first) + abs(y - a[i].second);
res += min(dx, dy);
}
return res;
}
long long solve2() {
if (n == 0) return 0;
int pt = 1;
long long res = 1e18;
for (int i = 0; i < z.size() - 1; ++i) {
if (pt <= i) pt = i + 1;
while(pt < (int)z.size() - 1 && calc(z[i], z[pt]) > calc(z[i], z[pt + 1])) ++pt;
res = min(res, calc(z[i], z[pt]));
}
return res;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) ans += abs(s - t);
else ++sz, a[sz] = make_pair(min(s, t), max(s, t));
}
n = sz;
for (int i = 1; i <= n; ++i) {
z.push_back(a[i].first);
z.push_back(a[i].second);
}
sort(z.begin(), z.end());
ans += n;
if (k == 1) ans += solve1(); else ans += min(solve1(), solve2());
printf("%lld\n", ans);
}
Compilation message (stderr)
bridge.cpp: In function 'long long int solve1()':
bridge.cpp:16:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < z.size(); ++i) {
^
bridge.cpp: In function 'long long int solve2()':
bridge.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < z.size() - 1; ++i) {
^
# | 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... |