#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define int long long
#define inf ((int)1e18)
using namespace std;
int solve(vector<pair<int,int> > v, int st, int nd) {
if(st == nd) return 0;
vector <int> p;
int ans = 0;
for(int i = st; i < nd; i++) {
int a = v[i].first, b = v[i].second;
p.push_back(a);
p.push_back(b);
}
sort(p.begin(), p.end());
int bridge = p[p.size() / 2];
for(auto it:p) {
ans += abs(bridge-it);
}
return ans;
}
int32_t main(){
fast
int k,n;
cin >> k >> n;
vector <pair<int, int> > p;
int add_ans = 0;
for(int i = 0; i < n; i++) {
char fuck1, fuck2;
int a, b;
cin >> fuck1 >> a >> fuck2 >> b;
if(fuck1 == fuck2) {
add_ans += abs(a - b);
}
else {
add_ans += 1;
p.push_back({min(a, b), max(a, b)});
}
}
sort(p.begin(), p.end(), [&](pair<int,int>a, pair<int,int>b) {
return (a.first + a.second) / 2 < (b.first + b.second) / 2;
});
int m = p.size();
int ans = inf;
for(int i = 0; i < m; i++) {
int st = p[i].first;
int tans = solve(p, i, m) + solve(p, 0, i);
ans = min(ans, tans);
}
cout << ans + add_ans << "\n";
}
Compilation message
bridge.cpp: In function 'int32_t main()':
bridge.cpp:48:7: warning: unused variable 'st' [-Wunused-variable]
48 | int st = p[i].first;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |