#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n;
cin >> k >> n;
vector<int> a;
vector<pair<ll, ll>> p;
ll res = 0, sum = 0;
for(int i = 0; i < n; i++) {
char c1, c2;
ll a1, a2;
cin >> c1 >> a1 >> c2 >> a2;
if(c1 == c2) {
res += abs(a1-a2);
}
else {
a.push_back(a1);
a.push_back(a2);
p.push_back({a1, a2});
sum += a1 + a2;
}
}
sort(a.begin(), a.end());
if(k == 1) {
int m = a.size()/2;
for(auto i: p) {
res += abs(a[m]-i.first) + abs(a[m]-i.second)+1;
}
}
else {
// dist from middle should be as small as possible
ll mx = sum;
for(int i = 0; i < a.size(); i++) {
ll cur = 0;
int m1 = i/2, m2 = (i+a.size())/2;
//cout << a[m1] << " " << a[m2] << "a\n";
for(auto i: p) {
ll m = (i.first+i.second)/2;
if(abs(a[m1]-m) < abs(a[m2]-m)) {
cur += abs(a[m1]-i.first) + abs(a[m1]-i.second)+1;
}
else {
cur += abs(a[m2]-i.first) + abs(a[m2]-i.second)+1;
}
}
//cout << cur << endl;
mx = min(mx, cur);
}
res += mx;
}
cout << res << "\n";
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
3188 KB |
Output is correct |
13 |
Correct |
24 ms |
3012 KB |
Output is correct |
14 |
Correct |
18 ms |
3012 KB |
Output is correct |
15 |
Correct |
15 ms |
2076 KB |
Output is correct |
16 |
Correct |
14 ms |
3016 KB |
Output is correct |
17 |
Correct |
16 ms |
3192 KB |
Output is correct |
18 |
Correct |
19 ms |
3016 KB |
Output is correct |
19 |
Correct |
22 ms |
3084 KB |
Output is correct |
20 |
Correct |
15 ms |
3016 KB |
Output is correct |
21 |
Correct |
20 ms |
3016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |