#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[n];
for (auto [i, j] : a) {
ans += abs(i-med);
ans += abs(j-med);
}
}
cout << ans+extra+n << 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
424 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
5872 KB |
Output is correct |
13 |
Correct |
35 ms |
7116 KB |
Output is correct |
14 |
Correct |
27 ms |
6088 KB |
Output is correct |
15 |
Correct |
23 ms |
4044 KB |
Output is correct |
16 |
Correct |
20 ms |
6724 KB |
Output is correct |
17 |
Correct |
23 ms |
7236 KB |
Output is correct |
18 |
Correct |
26 ms |
6824 KB |
Output is correct |
19 |
Correct |
27 ms |
7368 KB |
Output is correct |
20 |
Correct |
25 ms |
6868 KB |
Output is correct |
21 |
Correct |
26 ms |
6836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 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 |
- |