#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define ff first
#define ss second
int K, N, cnt;
ll pfx[100005], sfx[100005], cost;
pii P[100005];
vector<int> x;
void f(ll *f) {
priority_queue<pii, vector<pii>, greater<pii> > pq;
for (int i = 1; i <= N; ++i) x.push_back(P[i].ff), x.push_back(P[i].ss);
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
int s = 0;
for (int i = 1, j = 0; i <= N; ++i) {
f[i] = f[i - 1] + 2 * max({0, P[i].ff - x[j], x[j] - P[i].ss});
pq.emplace(P[i].ff, 2);
pq.emplace(P[i].ss, 2);
s -= 2;
while (pq.size() && pq.top().ff <= x[j]) {
s += pq.top().ss;
pq.pop();
}
while (s < 0) {
f[i] += 1ll * s * (x[j + 1] - x[j]);
j++;
while (pq.size() && pq.top().ff <= x[j]) {
s += pq.top().ss;
pq.pop();
}
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> K >> N;
for (int i = 0; i < N; ++i) {
char a, b; int c, d; cin >> a >> c >> b >> d;
cost += abs(c - d);
if (a != b) {
cost++;
if (c > d) swap(c, d);
P[++cnt] = {c, d};
}
}
N = cnt;
sort(P + 1, P + N + 1, [&](pii a, pii b) { return a.ff + a.ss < b.ff + b.ss; });
f(pfx);
if (K < 2) return !printf("%lld\n",pfx[N] + cost);
reverse(P + 1, P + N + 1);
for (int i = 1; i <= N; ++i) {
int t = -P[i].ff;
P[i].ff = -P[i].ss;
P[i].ss = t;
}
f(sfx);
ll mn = pfx[N];
for (int i = 1; i < N; ++i) mn = min(mn, pfx[i] + sfx[N - i]);
cout << mn + cost;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
42 ms |
4080 KB |
Output is correct |
13 |
Correct |
82 ms |
3952 KB |
Output is correct |
14 |
Correct |
64 ms |
3816 KB |
Output is correct |
15 |
Correct |
48 ms |
2296 KB |
Output is correct |
16 |
Correct |
48 ms |
3948 KB |
Output is correct |
17 |
Correct |
56 ms |
3968 KB |
Output is correct |
18 |
Correct |
61 ms |
3952 KB |
Output is correct |
19 |
Correct |
63 ms |
3952 KB |
Output is correct |
20 |
Correct |
47 ms |
3952 KB |
Output is correct |
21 |
Correct |
60 ms |
3952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
372 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
248 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
54 ms |
6112 KB |
Output is correct |
26 |
Correct |
84 ms |
6212 KB |
Output is correct |
27 |
Correct |
120 ms |
7688 KB |
Output is correct |
28 |
Correct |
126 ms |
8492 KB |
Output is correct |
29 |
Correct |
125 ms |
8428 KB |
Output is correct |
30 |
Correct |
65 ms |
4548 KB |
Output is correct |
31 |
Correct |
65 ms |
6880 KB |
Output is correct |
32 |
Correct |
77 ms |
8528 KB |
Output is correct |
33 |
Correct |
97 ms |
7992 KB |
Output is correct |
34 |
Correct |
85 ms |
8404 KB |
Output is correct |
35 |
Correct |
62 ms |
6940 KB |
Output is correct |
36 |
Correct |
82 ms |
8180 KB |
Output is correct |
37 |
Correct |
39 ms |
4424 KB |
Output is correct |