#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;
// for(int i=1; i<=all; i++){
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);
}
MN = min(MN, te);
// }
ans += seg.size();
cout << ans+MN << '\n';
} else {
vector <int> cc; cc.pb(-1); seg.pb({-1, -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;
if(all==0) MN = 0;
// cout << " p\n";
// for(auto in : seg) cout << in.fi << ' ' << in.se << " in\n";
for(int i=0; i<seg.size(); i++){ // ambil i awal
vector <int> vec;
for(int j=1; j<=i; j++){
vec.pb(seg[j].fi); vec.pb(seg[j].se);
}
sort(vec.begin(), vec.end());
vector <int> vec2;
for(int j=i+1; j<seg.size(); j++){
vec2.pb(seg[j].fi); vec2.pb(seg[j].se);
}
sort(vec2.begin(), vec2.end());
int te = 0;
int le = INF, ri = INF;
if(!vec.empty()) le = vec[vec.size()/2];
if(!vec2.empty()) ri = vec2[vec2.size()/2];
for(auto ed : seg){
int l = ed.fi, r = ed.se;
if(l==-1 && r==-1) continue;
te += min(abs(l-le)+abs(r-le), abs(l-ri)+abs(r-ri));
}
MN = min(MN, te);
// cout << i << ' ' << te << ' ' << le << ' ' << ri <<" te\n";
// for(auto ed : seg){
// int l = ed.fi, r = ed.se;
// if(l==-1 && r==-1) continue;
// cout << abs(l-le)+abs(r-le) << ' '<< abs(l-ri)+abs(r-ri) << " xx\n";
// }
// cout << " pp\n";
}
ans += seg.size()-1;
cout << ans+MN << '\n';
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:41:36: warning: unused variable 'las' [-Wunused-variable]
41 | int MN = INF, all = cc.size()-1, las = 1;
| ^~~
bridge.cpp:75:17: 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]
75 | for(int i=0; i<seg.size(); i++){ // ambil i awal
| ~^~~~~~~~~~~
bridge.cpp:83:20: 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]
83 | for(int j=i+1; j<seg.size(); j++){
| ~^~~~~~~~~~~
bridge.cpp:71:36: warning: unused variable 'las' [-Wunused-variable]
71 | int MN = INF, all = cc.size()-1, las = 1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |