#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
ll jogap, a[N], b[N];
int n, k;
char oy[N], of[N];
ll hasapla (ll x, ll y) {
ll ret = 0;
for (int i = 1; i <= n; ++i) {
if (oy[i] == of[i]) {
ret += abs (a[i] - b[i]);
}
else {
ll gosh = abs (a[i] - x) + abs (x - b[i]) + 1;
ll gosh1 = 2e18;
if (y != -1) {
gosh1 = abs (a[i] - y) + abs (y - b[i]) + 1;
}
ret += min (gosh, gosh1);
}
}
return ret;
}
int main () {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen ("input.txt", "r", stdin);
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
cin >> oy[i] >> a[i] >> of[i] >> b[i];
}
// return cout << n << " " << k, 0;
// for (int i = 1; i <= n; ++i) {
// cout << i << " --> " << oy[i] << " " << a[i] << " " << of[i] << " " << b[i] << "\n";
// }
// return 0;
jogap = 2e18;
for (int i = 1; i <= n; ++i) {
if (k == 1) jogap = min ({jogap, hasapla(a[i], -1), hasapla (b[i], -1)});
else {
for (int j = i + 1; j <= n; ++j) {
jogap = min ({jogap, hasapla(a[i], a[j]), hasapla (a[i], b[j]), hasapla (a[i], b[i]), hasapla (b[i], a[j]), hasapla (b[i], b[j]), hasapla (a[j], b[j])});
}
}
}
cout << jogap;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
500 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Execution timed out |
2058 ms |
2908 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |