#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
using ll = long long;
#define f first
#define s second
vector<pii> cross;
int N, K;
ll check1(int bridge) {
ll ret = 0;
for(auto [st, en]:cross) {
if(bridge<st) ret += (ll)st-bridge+en-bridge;
else if(bridge>en) ret += (ll)bridge-st+bridge-en;
else ret += (ll)en-st;
}
return ret;
}
ll one() {
vector<int> v;
for(auto [st, en]:cross) {
v.push_back(st);
v.push_back(en);
}
sort(v.begin(), v.end());
return min(check1(v[((int)v.size()-1)/2]), check1(v[(int)v.size()/2]));
}
ll val1[100005], val2[100005], suml, sumr;
priority_queue<int> l;
priority_queue<int, vector<int>, greater<int>> r;
void insert(int x) {
if(l.empty() || x<=l.top()) l.push(x), suml += x;
else r.push(x), sumr += x;
if(l.size()>r.size()+1) {
sumr += l.top();
suml -= l.top();
r.push(l.top());
l.pop();
}
if(r.size()>l.size()) {
suml += r.top();
sumr -= r.top();
l.push(r.top());
r.pop();
}
}
ll two() {
sort(cross.begin(), cross.end());
for(int i=0;i<cross.size()-1;i++) {
insert(cross[i].f); insert(cross[i].s);
val1[i] = sumr-suml;
}
while(!l.empty()) l.pop();
while(!r.empty()) r.pop();
suml = sumr = 0;
for(int i=cross.size()-2;i>=0;i--) {
insert(cross[i+1].f); insert(cross[i+1].s);
val2[i] = sumr-suml;
}
ll ret = one();
for(int i=0;i<cross.size()-1;i++) {
ret = min(ret, val1[i]+val2[i]);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin>>K>>N;
ll ans = 0;
for(int i=0;i<N;i++) {
char z1, z2;
int b1, b2;
cin>>z1>>b1>>z2>>b2;
if(b1>b2) swap(b1, b2);
if(z1==z2) {
ans += b2-b1;
} else {
cross.push_back({b1, b2});
}
}
if(cross.empty()) {
cout<<ans;
return 0;
}
ans += cross.size();
if(K==1) cout<<ans+one();
else cout<<ans+two();
}
Compilation message
bridge.cpp: In function 'll two()':
bridge.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0;i<cross.size()-1;i++) {
| ~^~~~~~~~~~~~~~~
bridge.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i=0;i<cross.size()-1;i++) {
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
496 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
16 ms |
3504 KB |
Output is correct |
13 |
Correct |
34 ms |
4808 KB |
Output is correct |
14 |
Correct |
25 ms |
3796 KB |
Output is correct |
15 |
Correct |
19 ms |
2768 KB |
Output is correct |
16 |
Correct |
20 ms |
4308 KB |
Output is correct |
17 |
Correct |
22 ms |
4816 KB |
Output is correct |
18 |
Correct |
26 ms |
4568 KB |
Output is correct |
19 |
Correct |
32 ms |
4708 KB |
Output is correct |
20 |
Correct |
22 ms |
4556 KB |
Output is correct |
21 |
Correct |
29 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |