#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 200001;
ll s1[N], s2[N], d1[N], d2[N];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int k, n, l, r;
ll ans = 0;
cin >> k >> n;
char c1, c2;
vector<int> li;
vector<pair<int, int>> a;
rep(i, n) {
cin >> c1 >> l >> c2 >> r;
if (l > r)
swap(l, r);
ans += r - l;
if (c1 == c2)
continue;
ans++;
li.push_back(l);
li.push_back(r);
a.push_back({l, r});
}
n = a.size();
sort(all(li));
li.resize(unique(all(li)) - li.begin());
sort(all(a), [](auto &p1, auto &p2) {return p1.first + p1.second < p2.first + p2.second;});
ll ca = 0, pos = 0, ck = 0;
rep(i, n) {
a[i].first = lower_bound(all(li), a[i].first) - li.begin();
a[i].second = lower_bound(all(li), a[i].second) - li.begin();
if (pos < a[i].first) {
ca += li[a[i].first] - li[pos];
ck--;
}
else if (a[i].second <= pos) {
ca += li[pos] - li[a[i].second];
ck++;
}
d1[a[i].first]++;
d1[a[i].second]++;
while (ck < 0) {
ca += ck * (li[pos + 1] - li[pos]);
pos++;
ck += d1[pos];
}
s1[i + 1] = ca;
}
ll mn = s1[n];
pos = li.size() - 1;
ck = ca = 0;
for (int i = n - 1; i >= 0; i--) {
if (pos <= a[i].first) {
ca += li[a[i].first] - li[pos];
ck++;
}
else if (a[i].second < pos) {
ca += li[pos] - li[a[i].second];
ck--;
}
d2[a[i].first]++;
d2[a[i].second]++;
while (ck < 0) {
ca += ck * (li[pos] - li[pos - 1]);
pos--;
ck += d2[pos];
}
if (k > 1)
mn = min(mn, s1[i] + ca);
}
cout << ans + mn * 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
27 ms |
3692 KB |
Output is correct |
13 |
Correct |
88 ms |
8420 KB |
Output is correct |
14 |
Correct |
57 ms |
4068 KB |
Output is correct |
15 |
Correct |
50 ms |
5100 KB |
Output is correct |
16 |
Correct |
37 ms |
4580 KB |
Output is correct |
17 |
Correct |
52 ms |
8420 KB |
Output is correct |
18 |
Correct |
57 ms |
8036 KB |
Output is correct |
19 |
Correct |
71 ms |
8164 KB |
Output is correct |
20 |
Correct |
39 ms |
4836 KB |
Output is correct |
21 |
Correct |
64 ms |
8164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
26 ms |
3692 KB |
Output is correct |
26 |
Correct |
40 ms |
3936 KB |
Output is correct |
27 |
Correct |
84 ms |
7476 KB |
Output is correct |
28 |
Correct |
92 ms |
8416 KB |
Output is correct |
29 |
Correct |
89 ms |
8416 KB |
Output is correct |
30 |
Correct |
47 ms |
4712 KB |
Output is correct |
31 |
Correct |
34 ms |
4580 KB |
Output is correct |
32 |
Correct |
55 ms |
8416 KB |
Output is correct |
33 |
Correct |
57 ms |
8160 KB |
Output is correct |
34 |
Correct |
73 ms |
8288 KB |
Output is correct |
35 |
Correct |
41 ms |
4836 KB |
Output is correct |
36 |
Correct |
64 ms |
8192 KB |
Output is correct |
37 |
Correct |
23 ms |
3684 KB |
Output is correct |