#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll ans,n,k,i,j,x,y,l,m,a[200005],z,mn=1e18,a1,a2,m1,m2;
vector<pair<ll,ll>> v;
char s,t;
bool cmp(pair<ll,ll> a,pair<ll,ll> b){
double x=(double)(a.ff+a.ss)/2;
double y=(double)(b.ff+b.ss)/2;
return x<y;
}
ordered_set p,q;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> k >> n;
for(i=1;i<=n;i++){
cin >> s >> x >> t >> y;
if(s==t) ans+=abs(x-y);
else{
ans++;
l++;
a[l]=x;
l++;
a[l]=y;
if(x>y) swap(x,y);
v.push_back({x,y});
}
}
sort(a+1,a+l+1);
x=a[(l+1)/2];
if(k==1){
for(i=1;i<=l;i++){
ans+=abs(x-a[i]);
}
cout << ans;
return 0;}
sort(v.begin(),v.end(),cmp);
if(v.size()==0){
cout << ans;
return 0;
}
if(v.size()==1){
cout << ans+v[0].ss-v[0].ff;
return 0;
}
p.insert(v[0].ff);
p.insert(v[0].ss);
for(i=1;i<v.size();i++){
q.insert(v[i].ff);
q.insert(v[i].ss);
}
m1=*p.find_by_order(p.size()/2);
m2=*q.find_by_order(q.size()/2);
for(auto it=p.begin();it!=p.end();it++) a1+=abs(*it-m1);
for(auto it=q.begin();it!=q.end();it++) a2+=abs(*it-m2);
mn=min(mn,a1+a2);
for(i=1;i<v.size()-1;i++){
ll x=v[i].ff,y=v[i].ss;
p.insert(x);
m=*p.find_by_order(p.size()/2);
a1+=abs(x-m1);
if(p.size()%2==1) a1-=abs(m1-m);
m1=m;
p.insert(y);
m=*p.find_by_order(p.size()/2);
a1+=abs(y-m1);
if(p.size()%2==1) a1-=abs(m1-m);
m1=m;
q.erase(q.upper_bound(x));
m=*q.find_by_order(q.size()/2);
if(q.size()%2==1) a2+=abs(m2-m);
a2-=abs(m2-x);
m2=m;
q.erase(q.upper_bound(y));
m=*q.find_by_order(q.size()/2);
if(q.size()%2==1) a2+=abs(m2-m);
a2-=abs(m2-y);
m2=m;
mn=min(mn,a1+a2);
}
cout << ans+mn;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:54:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(i=1;i<v.size();i++){
| ~^~~~~~~~~
bridge.cpp:63:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(i=1;i<v.size()-1;i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
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 |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
3796 KB |
Output is correct |
13 |
Correct |
31 ms |
3544 KB |
Output is correct |
14 |
Correct |
23 ms |
3544 KB |
Output is correct |
15 |
Correct |
22 ms |
2264 KB |
Output is correct |
16 |
Correct |
19 ms |
3552 KB |
Output is correct |
17 |
Correct |
22 ms |
3688 KB |
Output is correct |
18 |
Correct |
26 ms |
3544 KB |
Output is correct |
19 |
Correct |
31 ms |
3544 KB |
Output is correct |
20 |
Correct |
26 ms |
3532 KB |
Output is correct |
21 |
Correct |
27 ms |
3544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
856 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
400 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
500 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
434 ms |
16024 KB |
Output is correct |
26 |
Correct |
252 ms |
16840 KB |
Output is correct |
27 |
Correct |
390 ms |
17536 KB |
Output is correct |
28 |
Correct |
293 ms |
18260 KB |
Output is correct |
29 |
Correct |
359 ms |
18428 KB |
Output is correct |
30 |
Correct |
146 ms |
9688 KB |
Output is correct |
31 |
Correct |
498 ms |
17520 KB |
Output is correct |
32 |
Correct |
512 ms |
18120 KB |
Output is correct |
33 |
Correct |
160 ms |
17748 KB |
Output is correct |
34 |
Correct |
528 ms |
18112 KB |
Output is correct |
35 |
Correct |
491 ms |
17784 KB |
Output is correct |
36 |
Correct |
507 ms |
18120 KB |
Output is correct |
37 |
Correct |
492 ms |
16840 KB |
Output is correct |