#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define fillc(p, t, v) fill_n((t*) p, sizeof(p) / sizeof(t), v)
#define pb push_back
#define f first
#define s second
int K, N;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> K >> N;
ll base = 0;
vector<pair<ll, ll>> segs;
for(int i = 0; i < N; i++) {
char a, b;
ll x, y;
cin >> a >> x >> b >> y;
if(x > y) swap(x, y);
if(a == b) {
base += y - x;
}else{
base += y - x + 1;
segs.pb({x, y});
}
}
if(segs.size() <= 1) {
cout << base << '\n';
return 0;
}
sort(all(segs));
vector<pair<ll, ll>> es;
for(auto s : segs)
es.pb({s.f, 0}), es.pb({s.s, 1});
sort(all(es));
auto D = es;
D[D.size() - 1] = {0, 0};
for(int i = es.size() - 2; i >= 0; i--) {
D[i] = D[i + 1];
if(es[i].s == 0) {
D[i].f++;
D[i].s += es[i].f;
}
}
ll k = 0;
ll n = 0;
ll l = 0;
ll ans = INT64_MAX;
int i = 0;
for(auto p : es) {
k += n * (p.f - l);
if(p.s == 0) {
}else{
n++;
}
ll hm = 2 * (k + D[i].s - p.f * D[i].f);
ans = min(ans, hm);
l = p.f;
i++;
}
cout << base + ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
35 ms |
8548 KB |
Output is correct |
13 |
Correct |
63 ms |
8528 KB |
Output is correct |
14 |
Correct |
51 ms |
7780 KB |
Output is correct |
15 |
Correct |
36 ms |
5352 KB |
Output is correct |
16 |
Correct |
42 ms |
9976 KB |
Output is correct |
17 |
Correct |
46 ms |
10596 KB |
Output is correct |
18 |
Correct |
49 ms |
10212 KB |
Output is correct |
19 |
Correct |
51 ms |
10596 KB |
Output is correct |
20 |
Correct |
44 ms |
10236 KB |
Output is correct |
21 |
Correct |
49 ms |
10340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |