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) {
if (x == z.size()) x = 1e9; else x = z[x];
if (y == z.size()) y = 1e9; else y = z[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;
long long res = 1e18;
for (int i = 0; i < z.size(); ++i) {
int l = 0, r = z.size() - 1;
while(l < r) {
int mid = ((l + r) >> 1);
if (calc(i, mid) > calc(i, mid + 1)) l = mid + 1;
else r = mid;
}
res = min(res, calc(i, l));
}
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 calc(int, int)':
bridge.cpp:23:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (x == z.size()) x = 1e9; else x = z[x];
^
bridge.cpp:24:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (y == z.size()) y = 1e9; else y = z[y];
^
bridge.cpp: In function 'long long int solve2()':
bridge.cpp:37:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < z.size(); ++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... |