#line 1 "Palembang_Bridges.cpp"
#include <bits/stdc++.h>
#define int std::int64_t
#ifdef local
#define dbg(__VA_ARGS__) \
std::cerr << "[DBG|" << __LINE__ << "]: " << __VA_ARGS__ << std::endl;
#define cerr(__VA_ARGS__) std::cerr << __VA_ARGS__;
template <class T> void vec_out(const std::vector<T> &cc) {
dbg("vector: ");
for (auto x : cc) {
cerr(x << " ");
}
cerr("\n");
}
#else
#define dbg(...)
#define cerr(...)
template <class T> void vec_out(const std::vector<T> &cc) {}
#endif
using namespace std;
using ll = long long;
const int mod = ll(1e9) + 7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in
// c++ its -1 but its suppose to be 10^9+6
const int N = ll(2e5) + 100;
const long long inf = 5e18;
int k, n, m;
pair<int, int> point[N];
#define ff first
#define ss second
bool cmp(pair<int, int> a, pair<int, int> b) {
return (a.ff + a.ss) < (b.ff + b.ss);
}
#define all(a) a.begin(), a.end()
#define pb push_back
vector<int> get(int l, int r) {
if (l > r)
return {};
vector<int> res;
for (int i = l; i <= r; i++)
res.pb(point[i].ff), res.pb(point[i].ss);
sort(all(res));
return res;
}
std::int32_t main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> k >> n;
long long ex = 0;
for (int i = 1; i <= n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (s > t)
swap(s, t);
if (p == q)
ex += t - s;
else {
ex++;
point[++m] = {s, t};
}
}
sort(point + 1, point + m + 1, cmp);
long long ans = 0;
if (k == 1) {
vector<int> pos = get(1, m);
int mid = (pos.size()) / 2;
for (auto x : pos)
ans += abs(pos[mid] - x);
} else if (k == 2 && m) {
ans = inf;
for (int i = 1; i <= m; i++) {
vector<int> pos1 = get(1, i), pos2 = get(i + 1, m);
int mid1 = (pos1.size()) / 2, mid2 = (pos2.size()) / 2;
long long res = 0;
for (auto x : pos1)
res += abs(pos1[mid1] - x);
for (auto x : pos2)
res += abs(pos2[mid2] - x);
ans = min(ans, res);
}
}
cout << ans + ex;
/*
int K, N;
std::cin >> K >> N;
std::vector<std::tuple<char, std::int64_t, char, std::int64_t>> PSQT(N);
for (auto &[P, S, Q, T] : PSQT) {
std::cin >> P >> S >> Q >> T;
}
std::int64_t ans = 0;
std::vector<std::pair<std::int64_t, std::int64_t>> HW;
for (auto [P, S, Q, T] : PSQT) {
if (S > T) {
std::swap(S, T);
}
if (P == Q) {
ans += std::abs(T - S);
continue;
}
// the roads
ans++;
HW.emplace_back(S, T);
}
std::sort(HW.begin(), HW.end(), [](const auto &x, const auto &y) {
return (x.first + x.second) < (y.first + y.second);
});
auto calc = [&](std::int64_t S, std::int64_t T, std::int64_t x) {
return std::abs(S - x) + std::abs(T - x) + 1;
};
auto get = [&](int l, int r) {
if (l > r) {
return std::vector<std::int64_t>{};
}
std::vector<std::int64_t> pts;
for (int i = l; i <= r; i++) {
auto &&[H, W] = HW[i];
pts.push_back(H);
pts.push_back(W);
}
std::sort(pts.begin(), pts.end());
return pts;
};
if (K == 1) {
// find median as that is optimal for the
// for all sum abs(S - x) + abs(T - x)
auto pts = get(0, HW.size() - 1);
auto mid = pts.size() / 2;
mid--;
for (auto x : pts) {
ans += std::abs(x - pts[mid]);
}
std::cout << ans << "\n";
return 0;
}
// split at some point i
// then using same median technique as in K = 1
// then we get 2 sub vectors => in each find median (still optimal) and then
// add them up, thus giving you the 2 bridges
std::int64_t res = LLONG_MAX;
for (int f = 0; f < HW.size(); f++) {
auto fir = get(0, f);
auto sec = get(f + 1, HW.size() - 1);
if (fir.empty() || sec.empty()) {
continue;
}
vec_out(fir);
vec_out(sec);
auto m0 = fir.size() / 2;
m0--;
auto m1 = sec.size() / 2;
m1--;
auto md0 = fir[m0];
auto md1 = fir[m1];
std::int64_t tmp = 0;
// for (auto [H, W] : HW) {
// tmp += std::min(calc(H, W, md0), calc(H, W, md1));
// }
for (auto x : fir) {
tmp += std::abs(x - fir[m0]);
}
for (auto x : sec) {
tmp += std::abs(x - sec[m1]);
}
res = std::min(res, tmp);
}
ans += res;
std::cout << ans << "\n";
*/
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
20 ms |
4860 KB |
Output is correct |
13 |
Correct |
41 ms |
4868 KB |
Output is correct |
14 |
Correct |
37 ms |
4824 KB |
Output is correct |
15 |
Correct |
23 ms |
3800 KB |
Output is correct |
16 |
Correct |
21 ms |
4820 KB |
Output is correct |
17 |
Correct |
26 ms |
4752 KB |
Output is correct |
18 |
Correct |
30 ms |
4860 KB |
Output is correct |
19 |
Correct |
32 ms |
4848 KB |
Output is correct |
20 |
Correct |
24 ms |
4820 KB |
Output is correct |
21 |
Correct |
29 ms |
4852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
500 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
19 ms |
776 KB |
Output is correct |
14 |
Correct |
63 ms |
348 KB |
Output is correct |
15 |
Correct |
76 ms |
516 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
21 ms |
852 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
21 ms |
348 KB |
Output is correct |
20 |
Correct |
21 ms |
344 KB |
Output is correct |
21 |
Correct |
53 ms |
520 KB |
Output is correct |
22 |
Correct |
21 ms |
348 KB |
Output is correct |
23 |
Correct |
19 ms |
344 KB |
Output is correct |
24 |
Correct |
20 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
19 ms |
348 KB |
Output is correct |
14 |
Correct |
64 ms |
528 KB |
Output is correct |
15 |
Correct |
74 ms |
532 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
18 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
20 ms |
548 KB |
Output is correct |
20 |
Correct |
22 ms |
348 KB |
Output is correct |
21 |
Correct |
53 ms |
536 KB |
Output is correct |
22 |
Correct |
20 ms |
516 KB |
Output is correct |
23 |
Correct |
21 ms |
592 KB |
Output is correct |
24 |
Correct |
20 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
2084 ms |
7492 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |