#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int k, n, mn = LONG_LONG_MAX, sm;
cin >> k >> n;
set<int> st;
vector<pair<pair<char, int>, pair<char, int>>> vec(n);
for (int i = 0; i < n; ++i) {
cin >> vec[i].first.first >> vec[i].first.second >> vec[i].second.first >> vec[i].second.second;
st.insert(vec[i].first.second);
st.insert(vec[i].second.second);
}
if (k == 1) {
for (int i: st) {
sm = 0;
for (int j = 0; j < n; ++j) {
if (vec[j].first.first == vec[j].second.first) sm += abs(vec[j].first.second - vec[j].second.second);
else sm += abs(i - vec[j].second.second) + abs(vec[j].first.second - i) + 1;
}
mn = min(sm, mn);
}
}
else {
for (int i : st) {
for (int l : st) {
sm = 0;
for (int j = 0; j < n; ++j) {
if (vec[j].first.first == vec[j].second.first) sm += abs(vec[j].first.second - vec[j].second.second);
else sm += min(abs(i - vec[j].second.second) + abs(vec[j].first.second - i) + 1, abs(l - vec[j].second.second) + abs(vec[j].first.second - l) + 1);
}
mn = min(sm, mn);
}
}
}
cout << mn;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
556 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
34 ms |
3416 KB |
Output is correct |
13 |
Execution timed out |
2041 ms |
12872 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
12 ms |
440 KB |
Output is correct |
9 |
Correct |
12 ms |
448 KB |
Output is correct |
10 |
Correct |
11 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
12 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
344 KB |
Output is correct |
10 |
Correct |
11 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
29 ms |
344 KB |
Output is correct |
15 |
Execution timed out |
2063 ms |
348 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
448 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
12 ms |
448 KB |
Output is correct |
9 |
Correct |
16 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
28 ms |
344 KB |
Output is correct |
15 |
Execution timed out |
2053 ms |
352 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |