#include <bits/stdc++.h>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5+5;
long suml, sumr;
priority_queue<long> L;
priority_queue<long, vector<long>, greater<long> > R;
void add(long x) {
if(L.empty() || x <= L.top()) {
L.emplace(x), suml += x;
int now = L.size() + R.size();
if(L.size() > (now + 1) / 2) {
long t = L.top(); L.pop();
suml -= t;
R.emplace(t), sumr += t;
}
} else {
R.emplace(x), sumr += x;
int now = L.size() + R.size();
if(R.size() > (now + 1) / 2) {
long t = R.top(); R.pop();
sumr -= t;
L.emplace(t), suml += t;
}
}
}
int k, n;
long ans, dpl[N], dpr[N];
vector<pii> v;
int main() {
scanf("%d %d", &k, &n);
for(int i = 1; i <= n; i++) {
char a, c; int b, d;
scanf(" %c %d %c %d", &a, &b, &c, &d);
if(b > d) swap(b, d);
if(a == c) ans += d - b;
else v.emplace_back(b, d);
}
n = v.size(), ans += n;
if(!v.empty()) {
sort(v.begin(), v.end(), [&](const pii &a, const pii &b) {
return a.x + a.y < b.x + b.y;
});
for(int i = 1; i <= n; i++) {
add(v[i-1].x), add(v[i-1].y);
int now = L.size() + R.size();
long b = L.size() == (now + 1) / 2 ? L.top() : R.top();
dpl[i] = (b * L.size() - suml) + (sumr - b * R.size());
}
L = priority_queue<long>();
R = priority_queue<long, vector<long>, greater<long> >();
suml = sumr = 0;
for(int i = n; i; i--) {
add(v[i-1].x), add(v[i-1].y);
int now = L.size() + R.size();
long b = L.size() == (now + 1) / 2 ? L.top() : R.top();
dpr[i] = (b * L.size() - suml) + (sumr - b * R.size());
}
if(k == 1) ans += dpl[n];
else {
long tmp = 2e18;
for(int i = 0; i <= n; i++) tmp = min(tmp, dpl[i] + dpr[i+1]);
ans += tmp;
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
bridge.cpp: In function 'void add(long long int)':
bridge.cpp:20:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(L.size() > (now + 1) / 2) {
~~~~~~~~~^~~~~~~~~~~~~~~
bridge.cpp:28:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(R.size() > (now + 1) / 2) {
~~~~~~~~~^~~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:57:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
long b = L.size() == (now + 1) / 2 ? L.top() : R.top();
~~~~~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:66:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
long b = L.size() == (now + 1) / 2 ? L.top() : R.top();
~~~~~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &k, &n);
~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c %d %c %d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 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 |
7 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 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 |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
50 ms |
5748 KB |
Output is correct |
13 |
Correct |
79 ms |
5752 KB |
Output is correct |
14 |
Correct |
60 ms |
5296 KB |
Output is correct |
15 |
Correct |
50 ms |
3164 KB |
Output is correct |
16 |
Correct |
50 ms |
5752 KB |
Output is correct |
17 |
Correct |
104 ms |
5768 KB |
Output is correct |
18 |
Correct |
51 ms |
5388 KB |
Output is correct |
19 |
Correct |
83 ms |
5872 KB |
Output is correct |
20 |
Correct |
67 ms |
5876 KB |
Output is correct |
21 |
Correct |
89 ms |
5740 KB |
Output is correct |
# |
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 |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
412 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 |
3 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 |
3 ms |
428 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
4 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 |
256 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 |
356 KB |
Output is correct |
6 |
Correct |
3 ms |
256 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 |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
43 ms |
6388 KB |
Output is correct |
26 |
Correct |
76 ms |
6624 KB |
Output is correct |
27 |
Correct |
83 ms |
7360 KB |
Output is correct |
28 |
Correct |
82 ms |
8024 KB |
Output is correct |
29 |
Correct |
84 ms |
8008 KB |
Output is correct |
30 |
Correct |
45 ms |
4096 KB |
Output is correct |
31 |
Correct |
47 ms |
7428 KB |
Output is correct |
32 |
Correct |
92 ms |
8060 KB |
Output is correct |
33 |
Correct |
50 ms |
7160 KB |
Output is correct |
34 |
Correct |
86 ms |
7920 KB |
Output is correct |
35 |
Correct |
54 ms |
7544 KB |
Output is correct |
36 |
Correct |
69 ms |
7760 KB |
Output is correct |
37 |
Correct |
51 ms |
6476 KB |
Output is correct |