#include <bits/stdc++.h>
using ll = long long;
using std::vector;
template <class T>
constexpr T inf = std::numeric_limits<T>::max() / 2;
class slope_trick {
std::priority_queue<int> left;
std::priority_queue<int, vector<int>, std::greater<>> right;
ll min;
public:
slope_trick() : left(), right(), min(0) {}
void add(const int x) {
if (!left.empty() and x < left.top()) {
min += left.top() - x;
left.push(x);
right.push(left.top());
left.pop();
} else {
right.push(x);
}
if (!right.empty() and x > right.top()) {
min += x - right.top();
right.push(x);
left.push(right.top());
right.pop();
} else {
left.push(x);
}
}
ll minimum() const {
return min;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int K, N;
std::cin >> K >> N;
vector<int> S, T;
ll must = 0;
while (N--) {
char p, q;
int a, b;
std::cin >> p >> a >> q >> b;
if (p == q) {
must += std::abs(a - b);
} else {
must += 1;
S.push_back(a);
T.push_back(b);
}
}
N = (int)S.size();
vector<int> ord(N);
std::iota(ord.begin(), ord.end(), 0);
std::sort(ord.begin(), ord.end(), [&](const int i, const int j) {
return S[i] + T[i] < S[j] + T[j];
});
vector<ll> pref(N + 1), suff(N + 1);
slope_trick slope;
for (int i = 0; i < N; ++i) {
const int k = ord[i];
slope.add(S[k]);
slope.add(T[k]);
pref[i + 1] = slope.minimum();
}
slope = slope_trick();
for (int i = N - 1; i >= 0; --i) {
const int k = ord[i];
slope.add(S[k]);
slope.add(T[k]);
suff[i] = slope.minimum();
}
if (K == 1) {
std::cout << must + pref[N] << '\n';
} else {
ll min = inf<ll>;
for (int i = 0; i <= N; ++i) {
min = std::min(min, pref[i] + suff[i]);
}
std::cout << must + min << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
392 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
324 KB |
Output is correct |
12 |
Correct |
41 ms |
6688 KB |
Output is correct |
13 |
Correct |
102 ms |
8328 KB |
Output is correct |
14 |
Correct |
80 ms |
6748 KB |
Output is correct |
15 |
Correct |
57 ms |
5184 KB |
Output is correct |
16 |
Correct |
47 ms |
7600 KB |
Output is correct |
17 |
Correct |
71 ms |
8232 KB |
Output is correct |
18 |
Correct |
81 ms |
7848 KB |
Output is correct |
19 |
Correct |
78 ms |
8344 KB |
Output is correct |
20 |
Correct |
47 ms |
7804 KB |
Output is correct |
21 |
Correct |
74 ms |
7980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
36 ms |
7296 KB |
Output is correct |
26 |
Correct |
76 ms |
6908 KB |
Output is correct |
27 |
Correct |
109 ms |
8032 KB |
Output is correct |
28 |
Correct |
107 ms |
8236 KB |
Output is correct |
29 |
Correct |
104 ms |
8724 KB |
Output is correct |
30 |
Correct |
55 ms |
4284 KB |
Output is correct |
31 |
Correct |
46 ms |
7624 KB |
Output is correct |
32 |
Correct |
75 ms |
8268 KB |
Output is correct |
33 |
Correct |
76 ms |
7968 KB |
Output is correct |
34 |
Correct |
81 ms |
8224 KB |
Output is correct |
35 |
Correct |
47 ms |
7832 KB |
Output is correct |
36 |
Correct |
76 ms |
8076 KB |
Output is correct |
37 |
Correct |
26 ms |
6672 KB |
Output is correct |