#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
void solve() {
int k, n;
cin >> k >> n;
int add = 0;
vector<int> coords;
vector<pair<int, int>> needs;
for (int i = 0; i < n; i++) {
char t1, t2;
int x1, x2;
cin >> t1 >> x1 >> t2 >> x2;
if (t1 == t2) {
add += abs(x1 - x2);
} else {
add++;
if (t1 == 'B') {
swap(x1, x2);
}
coords.push_back(x1);
coords.push_back(x2);
needs.emplace_back(x1, x2);
}
}
int cs = coords.size();
if (cs == 0) {
cout << add;
return;
}
sort(coords.begin(), coords.end());
if (k == 1) {
int x = coords[cs / 2];
int res = 0;
for (int i = 0; i < cs; i++) {
res += abs(coords[i] - x);
}
cout << res + add;
} else {
auto f = [&] (int l, int r) {
l = coords[l];
r = coords[r];
int res = 0;
for (int i = 0; i < cs / 2; i++) {
res += min(abs(l - needs[i].first) + abs(l - needs[i].second), abs(r - needs[i].first) + abs(r - needs[i].second));
}
return res;
};
int best = inf;
for (int i = 0; i < cs; i++) {
int l = 0, r = i;
while (r - l > 3) {
int m1 = l + (r - l) / 3;
int m2 = r - (r - l) / 3;
if (f(m1, i) > f(m2, i)) {
l = m1;
} else {
r = m2;
}
}
for (int j = l; j <= r; j++) {
best = min(best, f(j, i));
}
}
cout << best + add;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
22 ms |
4376 KB |
Output is correct |
13 |
Correct |
43 ms |
6052 KB |
Output is correct |
14 |
Correct |
33 ms |
4648 KB |
Output is correct |
15 |
Correct |
24 ms |
3624 KB |
Output is correct |
16 |
Correct |
34 ms |
5304 KB |
Output is correct |
17 |
Correct |
30 ms |
5944 KB |
Output is correct |
18 |
Correct |
37 ms |
5684 KB |
Output is correct |
19 |
Correct |
40 ms |
5980 KB |
Output is correct |
20 |
Correct |
29 ms |
5552 KB |
Output is correct |
21 |
Correct |
39 ms |
5676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
316 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
324 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
180 ms |
340 KB |
Output is correct |
14 |
Correct |
182 ms |
340 KB |
Output is correct |
15 |
Correct |
183 ms |
460 KB |
Output is correct |
16 |
Correct |
19 ms |
320 KB |
Output is correct |
17 |
Correct |
51 ms |
360 KB |
Output is correct |
18 |
Correct |
25 ms |
460 KB |
Output is correct |
19 |
Correct |
190 ms |
376 KB |
Output is correct |
20 |
Correct |
200 ms |
392 KB |
Output is correct |
21 |
Correct |
193 ms |
340 KB |
Output is correct |
22 |
Correct |
183 ms |
380 KB |
Output is correct |
23 |
Correct |
181 ms |
376 KB |
Output is correct |
24 |
Correct |
185 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
320 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
320 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
189 ms |
340 KB |
Output is correct |
14 |
Correct |
179 ms |
368 KB |
Output is correct |
15 |
Correct |
193 ms |
340 KB |
Output is correct |
16 |
Correct |
16 ms |
340 KB |
Output is correct |
17 |
Correct |
48 ms |
328 KB |
Output is correct |
18 |
Correct |
23 ms |
340 KB |
Output is correct |
19 |
Correct |
196 ms |
376 KB |
Output is correct |
20 |
Correct |
188 ms |
380 KB |
Output is correct |
21 |
Correct |
198 ms |
340 KB |
Output is correct |
22 |
Correct |
192 ms |
340 KB |
Output is correct |
23 |
Correct |
190 ms |
340 KB |
Output is correct |
24 |
Correct |
199 ms |
376 KB |
Output is correct |
25 |
Execution timed out |
2077 ms |
4372 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |