#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
int n, k;
ll same, cross, lSum = 0, rSum = 0;
vector<pii> diff; vector<ll> prefix, suffix;
priority_queue<int> l;
priority_queue<int, vector<int>, greater<int>> r;
bool cmp(pii a, pii b){
return a.first+a.second < b.first+b.second;
}
void update(int x){
if (l.size() <= r.size()){
r.push(x); rSum += x;
l.push(r.top()); lSum += r.top();
rSum -= r.top(); r.pop();
}
else{
l.push(x); lSum += x;
r.push(l.top()); rSum += l.top();
lSum -= l.top(); l.pop();
}
}
int main(){
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 += abs(x-y);
else diff.push_back({x, y});
}
if (diff.size() == 0){
cout<<same; return 0;
}
sort(diff.begin(), diff.end(), cmp);
prefix.resize(diff.size()); suffix.resize(diff.size());
for (int i = 0; i < diff.size(); i++){
update(diff[i].first); update(diff[i].second);
prefix[i] = (rSum-l.top()*r.size()) + (l.top()*l.size()-lSum);
}
while (!l.empty()) l.pop();
while (!r.empty()) r.pop();
lSum = 0; rSum = 0;
for (int i = diff.size()-1; i >= 0; i--){
update(diff[i].first); update(diff[i].second);
suffix[i] = (rSum-l.top()*r.size()) + (l.top()*l.size()-lSum);
}
cross = prefix[diff.size()-1];
if (k == 2)
for (int i = 0; i < diff.size()-1; i++)
cross = min(cross, prefix[i]+suffix[i+1]);
cout<<same+cross+diff.size();
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < diff.size(); i++){
| ~~^~~~~~~~~~~~~
bridge.cpp:64:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < diff.size()-1; 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 |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
324 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
328 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
296 KB |
Output is correct |
11 |
Correct |
2 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 |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
320 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
296 KB |
Output is correct |
7 |
Correct |
3 ms |
300 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
280 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
93 ms |
4656 KB |
Output is correct |
13 |
Correct |
195 ms |
5296 KB |
Output is correct |
14 |
Correct |
143 ms |
4824 KB |
Output is correct |
15 |
Correct |
116 ms |
3860 KB |
Output is correct |
16 |
Correct |
127 ms |
5532 KB |
Output is correct |
17 |
Correct |
176 ms |
5300 KB |
Output is correct |
18 |
Correct |
165 ms |
5520 KB |
Output is correct |
19 |
Correct |
181 ms |
5608 KB |
Output is correct |
20 |
Correct |
163 ms |
5312 KB |
Output is correct |
21 |
Correct |
174 ms |
5344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 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 |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
2 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
308 KB |
Output is correct |
20 |
Correct |
2 ms |
300 KB |
Output is correct |
21 |
Correct |
2 ms |
304 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 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 |
204 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 |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
300 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
3 ms |
304 KB |
Output is correct |
20 |
Correct |
2 ms |
300 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
432 KB |
Output is correct |
24 |
Correct |
3 ms |
332 KB |
Output is correct |
25 |
Correct |
89 ms |
4600 KB |
Output is correct |
26 |
Correct |
162 ms |
4792 KB |
Output is correct |
27 |
Correct |
179 ms |
5596 KB |
Output is correct |
28 |
Correct |
203 ms |
6356 KB |
Output is correct |
29 |
Correct |
198 ms |
6208 KB |
Output is correct |
30 |
Correct |
106 ms |
3440 KB |
Output is correct |
31 |
Correct |
123 ms |
5468 KB |
Output is correct |
32 |
Correct |
172 ms |
6252 KB |
Output is correct |
33 |
Correct |
160 ms |
5812 KB |
Output is correct |
34 |
Correct |
178 ms |
6216 KB |
Output is correct |
35 |
Correct |
138 ms |
5776 KB |
Output is correct |
36 |
Correct |
208 ms |
5944 KB |
Output is correct |
37 |
Correct |
79 ms |
4576 KB |
Output is correct |