#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair <int , int>
#define F first
#define S second
#define L first
#define R second
const int N = 2e5 + 5, M = 2e3 + 5, inf = 1e18;
int prf[N], suf[N], prf_cost[N], suf_cost[N], ss[N], ps[N], cost[M][M], cnt[M][M];
vector <int> P;
vector <pii> seg;
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k, ans = 0; cin >> k >> n;
for (int i = 0; i < n; i++) {
char s, e; int x1, x2; cin >> s >> x1 >> e >> x2;
if (x1 > x2) swap(x1, x2);
ans += x2 - x1;
if (s != e) ans++, P.push_back(x1), P.push_back(x2), seg.push_back({x1, x2});
}
sort(all(P)), P.resize(unique(all(P)) - P.begin());
int m = sz(P);
for (pii p : seg) {
int l = lower_bound(all(P), p.L) - P.begin(), r = lower_bound(all(P), p.R) - P.begin();
suf[l]++, prf[r]++, ss[l] += p.L, ps[r] += p.R;
}
for (int i = m - 1; i >= 0; i--) suf[i] += suf[i + 1], ss[i] += ss[i + 1];
for (int i = 1; i < m; i++) prf[i] += prf[i - 1], ps[i] += ps[i - 1];
int res = inf;
for (int i = 0; i < m; i++)
prf_cost[i] = P[i] * prf[i] - ps[i], suf_cost[i] = ss[i] - P[i] * suf[i], res = min(res, prf_cost[i] + suf_cost[i]);
if (k == 2) {
for (pii p : seg) {
int l = lower_bound(all(P), p.L) - P.begin(), r = lower_bound(all(P), p.R) - P.begin();
cnt[l][r]++;
}
for (int len = 2; len <= m; len++) {
for (int l = 0, r = len - 1; r < m; l++, r++) {
if (len > 2) cost[l][r] = cost[l + 1][r - 1] + cnt[l + 1][r - 1];
cnt[l][r] += cnt[l][r - 1] + cnt[l + 1][r] - (l + 1 <= r - 1 ? cnt[l + 1][r - 1] : 0);
}
}
for (int i = 0; i < m; i++) for (int j = i + 1; j < m; j++) {
//res = min(res, prf_cost[i] + suf_cost[j] + cost[i][j]);
int x = P[i], y = P[j], sum = prf_cost[i] + suf_cost[j];
for (pii p : seg) if (x <= p.L && p.R <= y) sum += min(p.L - x, y - p.R);
res = min(res, sum);
}
}
if (res == inf) res = 0;
cout << ans + res*2 << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
31 ms |
3812 KB |
Output is correct |
13 |
Correct |
95 ms |
13016 KB |
Output is correct |
14 |
Correct |
57 ms |
3920 KB |
Output is correct |
15 |
Correct |
58 ms |
7904 KB |
Output is correct |
16 |
Correct |
37 ms |
3692 KB |
Output is correct |
17 |
Correct |
60 ms |
13016 KB |
Output is correct |
18 |
Correct |
65 ms |
13136 KB |
Output is correct |
19 |
Correct |
93 ms |
12632 KB |
Output is correct |
20 |
Correct |
41 ms |
3800 KB |
Output is correct |
21 |
Correct |
70 ms |
13136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
9 ms |
2048 KB |
Output is correct |
5 |
Correct |
6 ms |
1280 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
2304 KB |
Output is correct |
9 |
Correct |
9 ms |
2304 KB |
Output is correct |
10 |
Correct |
12 ms |
2176 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
9 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
9 ms |
2048 KB |
Output is correct |
5 |
Correct |
6 ms |
1280 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
2304 KB |
Output is correct |
9 |
Correct |
9 ms |
2304 KB |
Output is correct |
10 |
Correct |
11 ms |
2176 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
9 ms |
2304 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
22 ms |
1280 KB |
Output is correct |
15 |
Execution timed out |
2085 ms |
44664 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
2048 KB |
Output is correct |
5 |
Correct |
6 ms |
1280 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
9 ms |
2304 KB |
Output is correct |
9 |
Correct |
8 ms |
2304 KB |
Output is correct |
10 |
Correct |
10 ms |
2048 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
2304 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
22 ms |
1280 KB |
Output is correct |
15 |
Execution timed out |
2083 ms |
44664 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |