#include <bits/stdc++.h>
using namespace std;
#define int long long int
void solve() {
int k, n, extra = 0, ans = 0;
cin >> k >> n;
vector<pair<int, int>> a;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) {
extra += abs(s-t);
continue;
}
if (p == 'A') {
a.push_back({s, t});
} else {
a.push_back({t, s});
}
}
n = a.size();
sort(a.begin(), a.end());
if (k == 1) {
vector<int> b;
for (int i = 0; i < n; i++) {
b.push_back(a[i].first);
b.push_back(a[i].second);
}
sort(b.begin(), b.end());
int med = 0;
if (b.size()) {
med = b[b.size()/2];
}
for (auto [i, j] : a) {
ans += abs(i-med);
ans += abs(j-med);
}
cout << ans+extra << endl;
} else {
cout << 0 << endl;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 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 |
- |