# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
118551 | 2019-06-19T08:00:02 Z | roseanne_pcy | Palembang Bridges (APIO15_bridge) | C++14 | 100 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 | 384 KB | Output is correct |
2 | Correct | 2 ms | 384 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 3 ms | 384 KB | Output is correct |
5 | Correct | 3 ms | 384 KB | Output is correct |
6 | Correct | 3 ms | 384 KB | Output is correct |
7 | Correct | 3 ms | 384 KB | Output is correct |
8 | Correct | 3 ms | 384 KB | Output is correct |
9 | Correct | 3 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 3 ms | 512 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 384 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 3 ms | 384 KB | Output is correct |
5 | Correct | 3 ms | 384 KB | Output is correct |
6 | Correct | 2 ms | 384 KB | Output is correct |
7 | Correct | 3 ms | 384 KB | Output is correct |
8 | Correct | 2 ms | 384 KB | Output is correct |
9 | Correct | 3 ms | 384 KB | Output is correct |
10 | Correct | 3 ms | 384 KB | Output is correct |
11 | Correct | 3 ms | 384 KB | Output is correct |
12 | Correct | 43 ms | 4464 KB | Output is correct |
13 | Correct | 93 ms | 5856 KB | Output is correct |
14 | Correct | 76 ms | 4536 KB | Output is correct |
15 | Correct | 54 ms | 3680 KB | Output is correct |
16 | Correct | 44 ms | 5356 KB | Output is correct |
17 | Correct | 82 ms | 6108 KB | Output is correct |
18 | Correct | 60 ms | 5524 KB | Output is correct |
19 | Correct | 91 ms | 6124 KB | Output is correct |
20 | Correct | 59 ms | 5612 KB | Output is correct |
21 | Correct | 83 ms | 5740 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 384 KB | Output is correct |
2 | Correct | 2 ms | 432 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 2 ms | 384 KB | Output is correct |
5 | Correct | 2 ms | 384 KB | Output is correct |
6 | Correct | 2 ms | 384 KB | Output is correct |
7 | Correct | 2 ms | 384 KB | Output is correct |
8 | Correct | 2 ms | 384 KB | Output is correct |
9 | Correct | 2 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 2 ms | 384 KB | Output is correct |
12 | Correct | 3 ms | 384 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 384 KB | Output is correct |
2 | Correct | 2 ms | 408 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 2 ms | 384 KB | Output is correct |
5 | Correct | 2 ms | 384 KB | Output is correct |
6 | Correct | 2 ms | 384 KB | Output is correct |
7 | Correct | 2 ms | 384 KB | Output is correct |
8 | Correct | 2 ms | 384 KB | Output is correct |
9 | Correct | 2 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 2 ms | 384 KB | Output is correct |
12 | Correct | 2 ms | 384 KB | Output is correct |
13 | Correct | 3 ms | 384 KB | Output is correct |
14 | Correct | 2 ms | 384 KB | Output is correct |
15 | Correct | 3 ms | 384 KB | Output is correct |
16 | Correct | 2 ms | 384 KB | Output is correct |
17 | Correct | 3 ms | 384 KB | Output is correct |
18 | Correct | 2 ms | 256 KB | Output is correct |
19 | Correct | 2 ms | 384 KB | Output is correct |
20 | Correct | 3 ms | 384 KB | Output is correct |
21 | Correct | 3 ms | 384 KB | Output is correct |
22 | Correct | 3 ms | 384 KB | Output is correct |
23 | Correct | 3 ms | 384 KB | Output is correct |
24 | Correct | 3 ms | 384 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 384 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 2 ms | 384 KB | Output is correct |
5 | Correct | 2 ms | 428 KB | Output is correct |
6 | Correct | 2 ms | 384 KB | Output is correct |
7 | Correct | 2 ms | 384 KB | Output is correct |
8 | Correct | 2 ms | 384 KB | Output is correct |
9 | Correct | 2 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 3 ms | 384 KB | Output is correct |
12 | Correct | 2 ms | 384 KB | Output is correct |
13 | Correct | 2 ms | 384 KB | Output is correct |
14 | Correct | 3 ms | 384 KB | Output is correct |
15 | Correct | 3 ms | 384 KB | Output is correct |
16 | Correct | 2 ms | 384 KB | Output is correct |
17 | Correct | 3 ms | 384 KB | Output is correct |
18 | Correct | 2 ms | 384 KB | Output is correct |
19 | Correct | 2 ms | 384 KB | Output is correct |
20 | Correct | 3 ms | 384 KB | Output is correct |
21 | Correct | 3 ms | 384 KB | Output is correct |
22 | Correct | 3 ms | 384 KB | Output is correct |
23 | Correct | 2 ms | 384 KB | Output is correct |
24 | Correct | 3 ms | 384 KB | Output is correct |
25 | Correct | 45 ms | 4460 KB | Output is correct |
26 | Correct | 69 ms | 4716 KB | Output is correct |
27 | Correct | 92 ms | 5392 KB | Output is correct |
28 | Correct | 94 ms | 6000 KB | Output is correct |
29 | Correct | 100 ms | 5912 KB | Output is correct |
30 | Correct | 50 ms | 3372 KB | Output is correct |
31 | Correct | 42 ms | 5356 KB | Output is correct |
32 | Correct | 79 ms | 5996 KB | Output is correct |
33 | Correct | 59 ms | 5524 KB | Output is correct |
34 | Correct | 87 ms | 5960 KB | Output is correct |
35 | Correct | 56 ms | 5612 KB | Output is correct |
36 | Correct | 83 ms | 5736 KB | Output is correct |
37 | Correct | 41 ms | 4588 KB | Output is correct |