# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
363427 | 2021-02-05T23:05:07 Z | luciocf | Palembang Bridges (APIO15_bridge) | C++14 | 369 ms | 15456 KB |
#include <bits/stdc++.h> #define ff first #define ss second using namespace std; typedef long long ll; typedef pair<int, int> pii; const int maxn = 1e5+10; const ll inf = 1e18+10; struct Mediana { multiset<int> lower, upper; ll cost_lower, cost_upper; void clear(void) { lower.clear(); upper.clear(); cost_lower = cost_upper = 0; } void add(int x) { int tot = lower.size() + upper.size(); if (tot%2) { if (x >= *upper.begin()) { lower.insert(*upper.begin()); cost_lower += 1ll*(*upper.begin()); cost_upper -= 1ll*(*upper.begin()); upper.erase(upper.begin()); upper.insert(x); cost_upper += 1ll*x; } else { lower.insert(x); cost_lower += 1ll*x; } } else { if (x >= *upper.begin()) { upper.insert(x); cost_upper += 1ll*x; } else { upper.insert(*lower.rbegin()); cost_upper += 1ll*(*lower.rbegin()); cost_lower -= 1ll*(*lower.rbegin()); lower.erase(lower.find(*lower.rbegin())); lower.insert(x); cost_lower += 1ll*x; } } } ll get(void) { int med = *upper.begin(); int sz_lower = lower.size(), sz_upper = upper.size(); return (1ll*sz_lower*med - 1ll*sz_upper*med + cost_upper - cost_lower); } } Med; char t[2][maxn]; int pos[2][maxn]; ll pref[maxn], suf[maxn]; int main(void) { int k, n; scanf("%d %d", &k, &n); for (int i = 1; i <= n; i++) scanf(" %c %d %c %d", &t[0][i], &pos[0][i], &t[1][i], &pos[1][i]); if (k == 1) { ll ans = 0; vector<int> v; for (int i = 1; i <= n; i++) { if (t[0][i] == t[1][i]) ans += 1ll*abs(pos[0][i]-pos[1][i]); else { v.push_back(pos[0][i]); v.push_back(pos[1][i]); } } if (v.size()) { sort(v.begin(), v.end()); int med = v[v.size()/2]; for (auto x: v) ans += 1ll*abs(x-med); ans += 1ll*v.size()/2; } printf("%lld\n", ans); return 0; } ll ini = 0; vector<pii> v; for (int i = 1; i <= n; i++) { if (t[0][i] == t[1][i]) ini += 1ll*abs(pos[0][i]-pos[1][i]); else v.push_back({min(pos[0][i], pos[1][i]), max(pos[0][i], pos[1][i])}); } if (v.size() == 0) { printf("%lld\n", ini); return 0; } sort(v.begin(), v.end(), [&] (pii a, pii b) {return 1ll*a.ff + 1ll*a.ss < 1ll*b.ff + 1ll*b.ss;}); Med.clear(); for (int i = 0; i < v.size(); i++) { Med.add(v[i].ff); Med.add(v[i].ss); pref[i] = Med.get(); } Med.clear(); for (int i = v.size()-1; i >= 0; i--) { Med.add(v[i].ff); Med.add(v[i].ss); suf[i] = Med.get(); } ll ans = min(pref[v.size()-1], suf[0]) + ini; for (int i = 0; i < v.size()-1; i++) ans = min(ans, pref[i] + suf[i+1] + ini); printf("%lld\n", ans + v.size()); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 364 KB | Output is correct |
2 | Correct | 0 ms | 364 KB | Output is correct |
3 | Correct | 1 ms | 364 KB | Output is correct |
4 | Correct | 1 ms | 364 KB | Output is correct |
5 | Correct | 1 ms | 364 KB | Output is correct |
6 | Correct | 1 ms | 364 KB | Output is correct |
7 | Correct | 1 ms | 364 KB | Output is correct |
8 | Correct | 1 ms | 364 KB | Output is correct |
9 | Correct | 1 ms | 364 KB | Output is correct |
10 | Correct | 1 ms | 364 KB | Output is correct |
11 | Correct | 1 ms | 384 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 364 KB | Output is correct |
2 | Correct | 1 ms | 364 KB | Output is correct |
3 | Correct | 1 ms | 364 KB | Output is correct |
4 | Correct | 1 ms | 364 KB | Output is correct |
5 | Correct | 1 ms | 364 KB | Output is correct |
6 | Correct | 1 ms | 364 KB | Output is correct |
7 | Correct | 1 ms | 364 KB | Output is correct |
8 | Correct | 1 ms | 364 KB | Output is correct |
9 | Correct | 1 ms | 364 KB | Output is correct |
10 | Correct | 1 ms | 364 KB | Output is correct |
11 | Correct | 1 ms | 364 KB | Output is correct |
12 | Correct | 35 ms | 2404 KB | Output is correct |
13 | Correct | 56 ms | 2404 KB | Output is correct |
14 | Correct | 42 ms | 2404 KB | Output is correct |
15 | Correct | 32 ms | 1512 KB | Output is correct |
16 | Correct | 40 ms | 2492 KB | Output is correct |
17 | Correct | 43 ms | 2404 KB | Output is correct |
18 | Correct | 49 ms | 2532 KB | Output is correct |
19 | Correct | 55 ms | 2404 KB | Output is correct |
20 | Correct | 43 ms | 2404 KB | Output is correct |
21 | Correct | 51 ms | 2404 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 364 KB | Output is correct |
2 | Correct | 1 ms | 492 KB | Output is correct |
3 | Correct | 1 ms | 364 KB | Output is correct |
4 | Correct | 1 ms | 364 KB | Output is correct |
5 | Correct | 1 ms | 492 KB | Output is correct |
6 | Correct | 1 ms | 364 KB | Output is correct |
7 | Correct | 1 ms | 364 KB | Output is correct |
8 | Correct | 1 ms | 364 KB | Output is correct |
9 | Correct | 1 ms | 364 KB | Output is correct |
10 | Correct | 1 ms | 364 KB | Output is correct |
11 | Correct | 1 ms | 364 KB | Output is correct |
12 | Correct | 1 ms | 364 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 384 KB | Output is correct |
2 | Correct | 1 ms | 364 KB | Output is correct |
3 | Correct | 1 ms | 380 KB | Output is correct |
4 | Correct | 1 ms | 364 KB | Output is correct |
5 | Correct | 1 ms | 364 KB | Output is correct |
6 | Correct | 1 ms | 364 KB | Output is correct |
7 | Correct | 1 ms | 364 KB | Output is correct |
8 | Correct | 1 ms | 364 KB | Output is correct |
9 | Correct | 1 ms | 364 KB | Output is correct |
10 | Correct | 1 ms | 364 KB | Output is correct |
11 | Correct | 1 ms | 384 KB | Output is correct |
12 | Correct | 1 ms | 364 KB | Output is correct |
13 | Correct | 2 ms | 492 KB | Output is correct |
14 | Correct | 2 ms | 492 KB | Output is correct |
15 | Correct | 2 ms | 492 KB | Output is correct |
16 | Correct | 1 ms | 364 KB | Output is correct |
17 | Correct | 1 ms | 364 KB | Output is correct |
18 | Correct | 1 ms | 364 KB | Output is correct |
19 | Correct | 2 ms | 492 KB | Output is correct |
20 | Correct | 2 ms | 492 KB | Output is correct |
21 | Correct | 2 ms | 492 KB | Output is correct |
22 | Correct | 2 ms | 492 KB | Output is correct |
23 | Correct | 2 ms | 492 KB | Output is correct |
24 | Correct | 2 ms | 512 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 364 KB | Output is correct |
2 | Correct | 1 ms | 364 KB | Output is correct |
3 | Correct | 1 ms | 368 KB | Output is correct |
4 | Correct | 1 ms | 364 KB | Output is correct |
5 | Correct | 1 ms | 364 KB | Output is correct |
6 | Correct | 1 ms | 364 KB | Output is correct |
7 | Correct | 1 ms | 364 KB | Output is correct |
8 | Correct | 1 ms | 364 KB | Output is correct |
9 | Correct | 1 ms | 364 KB | Output is correct |
10 | Correct | 1 ms | 364 KB | Output is correct |
11 | Correct | 1 ms | 364 KB | Output is correct |
12 | Correct | 1 ms | 364 KB | Output is correct |
13 | Correct | 2 ms | 492 KB | Output is correct |
14 | Correct | 2 ms | 492 KB | Output is correct |
15 | Correct | 2 ms | 492 KB | Output is correct |
16 | Correct | 1 ms | 364 KB | Output is correct |
17 | Correct | 1 ms | 364 KB | Output is correct |
18 | Correct | 1 ms | 364 KB | Output is correct |
19 | Correct | 2 ms | 492 KB | Output is correct |
20 | Correct | 2 ms | 492 KB | Output is correct |
21 | Correct | 3 ms | 512 KB | Output is correct |
22 | Correct | 2 ms | 492 KB | Output is correct |
23 | Correct | 2 ms | 492 KB | Output is correct |
24 | Correct | 2 ms | 492 KB | Output is correct |
25 | Correct | 171 ms | 13792 KB | Output is correct |
26 | Correct | 282 ms | 14048 KB | Output is correct |
27 | Correct | 352 ms | 14816 KB | Output is correct |
28 | Correct | 369 ms | 15456 KB | Output is correct |
29 | Correct | 365 ms | 15456 KB | Output is correct |
30 | Correct | 135 ms | 8292 KB | Output is correct |
31 | Correct | 181 ms | 14688 KB | Output is correct |
32 | Correct | 163 ms | 15328 KB | Output is correct |
33 | Correct | 151 ms | 15072 KB | Output is correct |
34 | Correct | 165 ms | 15456 KB | Output is correct |
35 | Correct | 163 ms | 15072 KB | Output is correct |
36 | Correct | 169 ms | 15072 KB | Output is correct |
37 | Correct | 150 ms | 13792 KB | Output is correct |