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;
void solve1() {
if (n == 0) return;
int med = z[z.size() / 2];
for (int i = 0; i < z.size(); ++i) {
ans += abs(med - z[i]);
}
}
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;
}
void solve2() {
if (n == 0) return;
int pt = 1;
long long res = 1e18;
for (int i = 0; i < z.size() - 1; ++i) {
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]));
}
ans += 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) solve1(); else solve2();
printf("%lld\n", ans);
}
Compilation message (stderr)
bridge.cpp: In function 'void solve1()':
bridge.cpp:15:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < z.size(); ++i) {
^
bridge.cpp: In function 'void solve2()':
bridge.cpp:34: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... |