#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int k, n;
ll same_side = 0, l = 0, r = 0, p[N], s[N], ans;
vector<pair<int,int>> v;
priority_queue<int> lf;
priority_queue<int, vector<int>, greater<int>> rg;
void push(int x)
{
if(lf.empty() || x <= lf.top()) {
lf.push(x);
l += x;
} else {
rg.push(x);
r += x;
}
if((int)lf.size() - 1 > rg.size()) {
int t = lf.top();
l -= t;
r += t;
lf.pop();
rg.push(t);
} else if(lf.size() < rg.size()) {
int t = rg.top();
r -= t;
l += t;
rg.pop();
lf.push(t);
}
}
int main()
{
cin.tie(0); ios_base::sync_with_stdio(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) {
same_side += abs(x - y);
} else {
v.push_back({x, y});
}
}
sort(v.begin(), v.end(), [&](auto x, auto y){
return x.first + x.second < y.first + y.second;
});
for(int i = 0; i < v.size(); i++) {
push(v[i].first);
push(v[i].second);
p[i] = r - l;
}
ans = p[v.size() - 1];
if(k == 1) {
cout << same_side + ans + v.size() << '\n';
return 0;
}
while(!lf.empty()) lf.pop();
while(!rg.empty()) rg.pop();
l = 0, r = 0;
for(int i = v.size() - 1; i >= 0; i--) {
push(v[i].first);
push(v[i].second);
ans = min(ans, r - l + (i == 0 ? 0 : p[i - 1]));
}
cout << same_side + ans + v.size() << '\n';
}
Compilation message
bridge.cpp: In function 'void push(int)':
bridge.cpp:22:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if((int)lf.size() - 1 > rg.size()) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:57:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
29 ms |
3456 KB |
Output is correct |
13 |
Correct |
48 ms |
3164 KB |
Output is correct |
14 |
Correct |
38 ms |
3084 KB |
Output is correct |
15 |
Correct |
27 ms |
2156 KB |
Output is correct |
16 |
Correct |
31 ms |
3244 KB |
Output is correct |
17 |
Correct |
37 ms |
3016 KB |
Output is correct |
18 |
Correct |
39 ms |
3068 KB |
Output is correct |
19 |
Correct |
48 ms |
3044 KB |
Output is correct |
20 |
Correct |
31 ms |
3520 KB |
Output is correct |
21 |
Correct |
40 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
33 ms |
3276 KB |
Output is correct |
26 |
Correct |
60 ms |
4376 KB |
Output is correct |
27 |
Correct |
78 ms |
5040 KB |
Output is correct |
28 |
Correct |
92 ms |
5704 KB |
Output is correct |
29 |
Correct |
85 ms |
5396 KB |
Output is correct |
30 |
Correct |
41 ms |
2984 KB |
Output is correct |
31 |
Correct |
40 ms |
4684 KB |
Output is correct |
32 |
Correct |
60 ms |
5484 KB |
Output is correct |
33 |
Correct |
62 ms |
5424 KB |
Output is correct |
34 |
Correct |
66 ms |
5532 KB |
Output is correct |
35 |
Correct |
42 ms |
5416 KB |
Output is correct |
36 |
Correct |
73 ms |
5156 KB |
Output is correct |
37 |
Correct |
29 ms |
3928 KB |
Output is correct |