#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1e5;
int n, k;
ll add, ans, tot, p[mxN];
vector<ar<int, 2>> v;
priority_queue<int> lo;
priority_queue<int, vector<int>, greater<int>> hi;
void ins(int x) {
if (lo.empty()||x<=lo.top())
lo.push(x), tot-=x;
else
hi.push(x), tot+=x;
if (lo.size()>hi.size()+1) {
int x=lo.top(); lo.pop();
tot+=2*x, hi.push(x);
}
if (hi.size()>lo.size()) {
int x=hi.top(); hi.pop();
tot-=2*x, lo.push(x);
}
}
ll get() {
return tot+(lo.size()-hi.size())*lo.top();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
for (int i=0; i<n; ++i) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a==b)
add+=abs(x-y);
else {
++add;
v.push_back({x, y});
}
}
if (v.empty()) {
cout << add;
return 0;
}
sort(v.begin(), v.end(), [](ar<int, 2> a, ar<int, 2> b) {return a[0]+a[1]<b[0]+b[1];});
for (int i=0; i<v.size(); ++i) {
ins(v[i][0]), ins(v[i][1]);
p[i]=get();
}
ans=p[v.size()-1];
if (k==2) {
tot=0;
priority_queue<int>().swap(lo);
priority_queue<int, vector<int>, greater<int>>().swap(hi);
for (int i=v.size()-1; i; --i) {
ins(v[i][0]), ins(v[i][1]);
ans=min(ans, p[i-1]+get());
}
}
cout << add+ans;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:53:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i=0; i<v.size(); ++i) {
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
31 ms |
4288 KB |
Output is correct |
13 |
Correct |
59 ms |
5492 KB |
Output is correct |
14 |
Correct |
46 ms |
4416 KB |
Output is correct |
15 |
Correct |
35 ms |
3452 KB |
Output is correct |
16 |
Correct |
40 ms |
4924 KB |
Output is correct |
17 |
Correct |
48 ms |
5372 KB |
Output is correct |
18 |
Correct |
45 ms |
4996 KB |
Output is correct |
19 |
Correct |
53 ms |
5384 KB |
Output is correct |
20 |
Correct |
39 ms |
5308 KB |
Output is correct |
21 |
Correct |
58 ms |
5184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
38 ms |
3852 KB |
Output is correct |
26 |
Correct |
57 ms |
4384 KB |
Output is correct |
27 |
Correct |
77 ms |
5144 KB |
Output is correct |
28 |
Correct |
77 ms |
5828 KB |
Output is correct |
29 |
Correct |
79 ms |
5436 KB |
Output is correct |
30 |
Correct |
41 ms |
3092 KB |
Output is correct |
31 |
Correct |
48 ms |
5016 KB |
Output is correct |
32 |
Correct |
62 ms |
5388 KB |
Output is correct |
33 |
Correct |
62 ms |
5472 KB |
Output is correct |
34 |
Correct |
74 ms |
5392 KB |
Output is correct |
35 |
Correct |
47 ms |
5292 KB |
Output is correct |
36 |
Correct |
66 ms |
5144 KB |
Output is correct |
37 |
Correct |
34 ms |
3900 KB |
Output is correct |