#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int MAXN = 2e5+10;
const int INF = 4e18+10;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
int K, n;
int ans;
vector <int> vec;
vector <pii> seg;
int pr[MAXN];
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> K >> n;
if(K==1){
exit(0);
vector <int> cc; cc.pb(-1);
for(int i=1; i<=n; i++){
char x, y; int a, b;
cin >> x >> a >> y >> b; a++; b++;
if(x==y) ans += abs(a-b);
else {
if(a > b) swap(a, b);
seg.pb({a, b});
cc.pb(a); cc.pb(b);
}
}
sort(seg.begin(), seg.end());
sort(cc.begin(), cc.end());
// cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
// cout << ans << " ans\n";
int MN = INF, all = cc.size()-1, las = 1;
int te = 0, le = cc[(all+1)/2];
for(auto ed : seg){
int l = ed.fi, r = ed.se;
te += abs(l-le)+abs(r-le);
}
ans += seg.size();
cout << ans+te << '\n';
} else {
vector <int> cc; cc.pb(-1);
for(int i=1; i<=n; i++){
char x, y; int a, b;
cin >> x >> a >> y >> b; a++; b++;
if(x==y) ans += abs(a-b);
else {
if(a > b) swap(a, b);
seg.pb({a, b});
cc.pb(a); cc.pb(b);
}
}
sort(seg.begin(), seg.end());
sort(cc.begin(), cc.end());
cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
// cout << ans << " ans\n";
int MN = INF, all = cc.size()-1, las = 1;
for(int i=1; i<=all; i++){
for(int j=i; j<=all; j++){
int te = 0, le = cc[i], ri = cc[j];
for(auto ed : seg){
int l = ed.fi, r = ed.se;
te += min(abs(l-le)+abs(r-le), abs(l-ri)+abs(r-ri));
}
MN = min(MN, te);
}
}
ans += seg.size();
cout << ans+MN << '\n';
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:41:7: warning: unused variable 'MN' [-Wunused-variable]
41 | int MN = INF, all = cc.size()-1, las = 1;
| ^~
bridge.cpp:41:36: warning: unused variable 'las' [-Wunused-variable]
41 | int MN = INF, all = cc.size()-1, las = 1;
| ^~~
bridge.cpp:68:36: warning: unused variable 'las' [-Wunused-variable]
68 | int MN = INF, all = cc.size()-1, las = 1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |