#include <bits/stdc++.h>
using namespace std;
#define ii pair<int,int>
const int maxn=1e5+7;
struct nguoi
{
int xo,yo,xt,yt;
};
nguoi a[maxn];
int k,n;
main()
{
cin>>k>>n;
vector<ii>v;
vector<int>tmp;
long long delt=0;
for(int i=1;i<=n;i++){
char xo,xt;
cin>>xo>>a[i].yo>>xt>>a[i].yt;
a[i].xo=(xo=='B');
a[i].xt=(xt=='B');
if(xo==xt){
delt+=abs(a[i].yt-a[i].yo);
}
else{
tmp.push_back(a[i].yo);
tmp.push_back(a[i].yt);
v.push_back({a[i].yo,a[i].yt});
}
}
long long ans=LLONG_MAX;
sort(tmp.begin(),tmp.end());
int to=tmp[tmp.size()/2];
long long s=0ll;
for(auto [x,y]:v){
s=s+((abs(x-to)+abs(y-to))+1);
}
ans=min(ans,s);
// if(tmp.size()/2-1>=0){
// to=tmp[tmp.size()/2-1];
// s=0ll;
// for(auto [x,y]:v)
// {
// s=s+((abs(x-to)+abs(y-to))+1);
// }
// ans=min(ans,s);
// }
// if(tmp.size()/2+1<tmp.size()){
// to=tmp[tmp.size()/2+1];
// s=0ll;
// for(auto [x,y]:v)
// {
// s=s+((abs(x-to)+abs(y-to))+1);
// }
// ans=min(ans,s);
// }
cout<<ans+delt;
return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
bridge.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main()
| ^~~~
bridge.cpp: In function 'int main()':
bridge.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for(auto [x,y]:v){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |