# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
389802 | milleniumEeee | Palembang Bridges (APIO15_bridge) | C++14 | 2073 ms | 5724 KiB |
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>
#define fr first
#define sc second
#define pii pair<int, int>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
const int MAXN = (int)2e5 + 5;
const int INF = 1e18;
pair <char, int> st[MAXN];
pair <char, int> fn[MAXN];
bool ok(int pos) {
return (st[pos].first == fn[pos].first);
}
signed main() {
fastInp;
int n, k;
cin >> k >> n;
for (int i = 1; i <= n; i++) {
cin >> st[i].fr >> st[i].sc >> fn[i].fr >> fn[i].sc;
}
if (k == 1) {
int side = 0;
int bridge = 0;
vector <int> x;
for (int i = 1; i <= n; i++) {
if (ok(i)) {
side += abs(st[i].sc - fn[i].sc);
} else {
x.pb(st[i].sc);
x.pb(fn[i].sc);
}
}
sort(all(x));
int opt = x[szof(x) / 2 - 1];
for (int el : x) {
bridge += abs(opt - el);
}
cout << side + szof(x) / 2 + bridge << endl;
}
else if (k == 2) {
int ans = INF;
vector <int> vec;
for (int i = 1; i <= n; i++) {
vec.pb(st[i].sc);
vec.pb(fn[i].sc);
}
int bb1, bb2;
for (int b1 : vec) {
for (int b2 : vec) {
int cur = 0;
for (int p = 1; p <= n; p++) {
if (ok(p)) {
cur += abs(st[p].sc - fn[p].sc);
} else {
int x = st[p].sc;
int y = fn[p].sc;
cur += 1 + min(abs(x - b1) + abs(y - b1), abs(x - b2) + abs(y - b2));
}
}
if (ans > cur) {
ans = cur;
bb1 = b1;
bb2 = b2;
}
}
}
cout << ans << endl;
//cout << "ans: " << ans << endl;
//cout << "b1: " << bb1 << endl;
//cout << "b2: " << bb2 << endl;
}
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message (stderr)
# | 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... |