# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
148094 | 2019-08-31T13:50:02 Z | WhipppedCream | Palembang Bridges (APIO15_bridge) | C++17 | 112 ms | 6124 KB |
#include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC target ("sse4") using namespace std; #define X first #define Y second #define pb push_back typedef pair<int, int> ii; typedef long long ll; const int maxn = 1e5+5; int k, n; ii foo[maxn]; ll base = 0; bool cmp(ii a, ii b) { return a.X+a.Y< b.X+b.Y; } ll v1[maxn], v2[maxn]; priority_queue<int> pq1; priority_queue<int, vector<int>, greater<int> > pq2; ll s1 = 0, s2 = 0; void proc(int x) { if(pq1.empty() && pq2.empty()) { pq1.push(x); s1 += x; } else if(pq1.empty()) { if(x< pq2.top()) pq1.push(x), s1 += x; else pq2.push(x), s2 += x; } else { if(x> pq1.top()) pq2.push(x), s2 += x; else pq1.push(x), s1 += x; } } void adjust() { if(pq1.size() == pq2.size()) return; if(pq1.size()> pq2.size()) { int x = pq1.top(); pq1.pop(); s1 -= x; pq2.push(x); s2 += x; } else { int x = pq2.top(); pq2.pop(); s2 -= x; pq1.push(x); s1 += x; } } int main() { scanf("%d %d", &k, &n); int m = 0; for(int i = 1; i<= n; i++) { char a, b; int x, y; scanf(" %c %d %c %d", &a, &x, &b, &y); if(a == b) { base += abs(x-y); continue; } if(abs(x)> abs(y)) swap(x, y); m++; foo[m] = {x, y}; } n = m; sort(foo+1, foo+n+1, cmp); for(int i = 1; i<= n; i++) { int x = foo[i].X, y = foo[i].Y; proc(x); proc(y); adjust(); v1[i] = s2-s1; } while(!pq1.empty()) pq1.pop(); s1 = 0; while(!pq2.empty()) pq2.pop(); s2 = 0; for(int i = n; i>= 1; i--) { int x = foo[i].X, y = foo[i].Y; proc(x); proc(y); adjust(); v2[i] = s2-s1; } ll best = 1e18; if(k == 1) best = v1[n]; else { best = min(v1[n], v2[1]); for(int i = 1; i<= n; i++) { best = min(best, v1[i]+v2[i+1]); } } printf("%lld\n", best+base+n); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 3 ms | 380 KB | Output is correct |
4 | Correct | 3 ms | 376 KB | Output is correct |
5 | Correct | 3 ms | 376 KB | Output is correct |
6 | Correct | 2 ms | 376 KB | Output is correct |
7 | Correct | 3 ms | 380 KB | Output is correct |
8 | Correct | 2 ms | 376 KB | Output is correct |
9 | Correct | 3 ms | 376 KB | Output is correct |
10 | Correct | 3 ms | 376 KB | Output is correct |
11 | Correct | 3 ms | 504 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 376 KB | Output is correct |
4 | Correct | 3 ms | 376 KB | Output is correct |
5 | Correct | 3 ms | 376 KB | Output is correct |
6 | Correct | 2 ms | 376 KB | Output is correct |
7 | Correct | 3 ms | 376 KB | Output is correct |
8 | Correct | 3 ms | 380 KB | Output is correct |
9 | Correct | 3 ms | 376 KB | Output is correct |
10 | Correct | 3 ms | 376 KB | Output is correct |
11 | Correct | 3 ms | 376 KB | Output is correct |
12 | Correct | 50 ms | 4588 KB | Output is correct |
13 | Correct | 112 ms | 5988 KB | Output is correct |
14 | Correct | 88 ms | 4608 KB | Output is correct |
15 | Correct | 64 ms | 3800 KB | Output is correct |
16 | Correct | 49 ms | 5356 KB | Output is correct |
17 | Correct | 93 ms | 6048 KB | Output is correct |
18 | Correct | 65 ms | 5480 KB | Output is correct |
19 | Correct | 101 ms | 5996 KB | Output is correct |
20 | Correct | 66 ms | 5612 KB | Output is correct |
21 | Correct | 96 ms | 5712 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 296 KB | Output is correct |
4 | Correct | 2 ms | 376 KB | Output is correct |
5 | Correct | 2 ms | 296 KB | Output is correct |
6 | Correct | 2 ms | 376 KB | Output is correct |
7 | Correct | 2 ms | 376 KB | Output is correct |
8 | Correct | 2 ms | 376 KB | Output is correct |
9 | Correct | 2 ms | 376 KB | Output is correct |
10 | Correct | 3 ms | 404 KB | Output is correct |
11 | Correct | 2 ms | 376 KB | Output is correct |
12 | Correct | 2 ms | 376 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 252 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 376 KB | Output is correct |
4 | Correct | 3 ms | 376 KB | Output is correct |
5 | Correct | 2 ms | 376 KB | Output is correct |
6 | Correct | 2 ms | 376 KB | Output is correct |
7 | Correct | 2 ms | 376 KB | Output is correct |
8 | Correct | 2 ms | 376 KB | Output is correct |
9 | Correct | 2 ms | 376 KB | Output is correct |
10 | Correct | 2 ms | 368 KB | Output is correct |
11 | Correct | 2 ms | 376 KB | Output is correct |
12 | Correct | 2 ms | 376 KB | Output is correct |
13 | Correct | 3 ms | 376 KB | Output is correct |
14 | Correct | 3 ms | 376 KB | Output is correct |
15 | Correct | 3 ms | 376 KB | Output is correct |
16 | Correct | 2 ms | 376 KB | Output is correct |
17 | Correct | 2 ms | 376 KB | Output is correct |
18 | Correct | 2 ms | 376 KB | Output is correct |
19 | Correct | 3 ms | 376 KB | Output is correct |
20 | Correct | 3 ms | 376 KB | Output is correct |
21 | Correct | 3 ms | 376 KB | Output is correct |
22 | Correct | 3 ms | 376 KB | Output is correct |
23 | Correct | 3 ms | 504 KB | Output is correct |
24 | Correct | 3 ms | 376 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 376 KB | Output is correct |
4 | Correct | 2 ms | 376 KB | Output is correct |
5 | Correct | 2 ms | 376 KB | Output is correct |
6 | Correct | 2 ms | 404 KB | Output is correct |
7 | Correct | 2 ms | 376 KB | Output is correct |
8 | Correct | 2 ms | 376 KB | Output is correct |
9 | Correct | 2 ms | 380 KB | Output is correct |
10 | Correct | 2 ms | 376 KB | Output is correct |
11 | Correct | 2 ms | 376 KB | Output is correct |
12 | Correct | 2 ms | 376 KB | Output is correct |
13 | Correct | 2 ms | 376 KB | Output is correct |
14 | Correct | 3 ms | 376 KB | Output is correct |
15 | Correct | 3 ms | 376 KB | Output is correct |
16 | Correct | 2 ms | 376 KB | Output is correct |
17 | Correct | 3 ms | 376 KB | Output is correct |
18 | Correct | 2 ms | 376 KB | Output is correct |
19 | Correct | 3 ms | 376 KB | Output is correct |
20 | Correct | 3 ms | 376 KB | Output is correct |
21 | Correct | 3 ms | 376 KB | Output is correct |
22 | Correct | 3 ms | 380 KB | Output is correct |
23 | Correct | 3 ms | 376 KB | Output is correct |
24 | Correct | 3 ms | 380 KB | Output is correct |
25 | Correct | 53 ms | 4456 KB | Output is correct |
26 | Correct | 79 ms | 4624 KB | Output is correct |
27 | Correct | 105 ms | 5292 KB | Output is correct |
28 | Correct | 110 ms | 5868 KB | Output is correct |
29 | Correct | 110 ms | 5884 KB | Output is correct |
30 | Correct | 58 ms | 3376 KB | Output is correct |
31 | Correct | 50 ms | 5356 KB | Output is correct |
32 | Correct | 94 ms | 6008 KB | Output is correct |
33 | Correct | 66 ms | 5772 KB | Output is correct |
34 | Correct | 103 ms | 6124 KB | Output is correct |
35 | Correct | 66 ms | 5556 KB | Output is correct |
36 | Correct | 100 ms | 5740 KB | Output is correct |
37 | Correct | 47 ms | 4456 KB | Output is correct |