#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define ll long long
#define pii pair <int, int>
#define sz size()
const int N = 2e5 + 5;
vector <ll> v;
char s[N], f[N];
ll a[N], b[N];
int k, n;
int main() {
// freopen ("input.txt", "r", stdin);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i] >> a[i] >> f[i] >> b[i];
if (s[i] != f[i]) {
v.pb (a[i]);
v.pb (b[i]);
}
}
sort (v.begin(), v.end());
if (k == 1) {
ll idx = 0;
if (!v.empty()) idx = v[(int)v.sz / 2];
ll sum = 0;
for (int i = 1; i <= n; ++i) {
if (s[i] == f[i]) {
sum += abs (a[i] - b[i]);
}
else {
sum += abs (idx - a[i]);
sum += abs (idx - b[i]);
sum++;
}
}
cout << sum;
}
else {
ll jog = 0;
for (int i = 1; i <= n; ++i) {
if (s[i] == f[i]) {
jog += abs (a[i] - b[i]);
}
}
ll sum = -1;
if (!v.empty())
for (int i = 0; i < (int)v.sz; ++i) {
for (int j = i + 1; j < (int)v.sz; ++j) {
ll idx = v[i], idx1 = v[j];
ll sum1 = 0;
for (int k = 1; k <= n; ++k) {
if (s[k] != f[k]) {
ll a1 = abs (idx - a[k]) + abs (idx - b[k]), b1 = abs (idx1 - a[k]) + abs (idx1 - b[k]);
sum1 += min (a1, b1);
sum1++;
}
}
if (sum == -1) sum = sum1;
else sum = min (sum, sum1);
}
}
if (sum == -1) sum = 0;
cout << jog + sum << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
17 ms |
3800 KB |
Output is correct |
13 |
Correct |
28 ms |
4824 KB |
Output is correct |
14 |
Correct |
21 ms |
4824 KB |
Output is correct |
15 |
Correct |
19 ms |
3804 KB |
Output is correct |
16 |
Correct |
27 ms |
3800 KB |
Output is correct |
17 |
Correct |
35 ms |
4820 KB |
Output is correct |
18 |
Correct |
36 ms |
3860 KB |
Output is correct |
19 |
Correct |
29 ms |
3800 KB |
Output is correct |
20 |
Correct |
19 ms |
4824 KB |
Output is correct |
21 |
Correct |
29 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
4 ms |
2528 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
2396 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
2396 KB |
Output is correct |
11 |
Correct |
5 ms |
2392 KB |
Output is correct |
12 |
Correct |
8 ms |
2528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
2396 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
2396 KB |
Output is correct |
11 |
Correct |
6 ms |
2396 KB |
Output is correct |
12 |
Correct |
6 ms |
2528 KB |
Output is correct |
13 |
Execution timed out |
2073 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
2396 KB |
Output is correct |
8 |
Correct |
5 ms |
2524 KB |
Output is correct |
9 |
Correct |
8 ms |
600 KB |
Output is correct |
10 |
Correct |
6 ms |
2528 KB |
Output is correct |
11 |
Correct |
6 ms |
2396 KB |
Output is correct |
12 |
Correct |
5 ms |
484 KB |
Output is correct |
13 |
Execution timed out |
2036 ms |
2392 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |