#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int k, n;
cin >> k >> n;
ll sum = 0;
vector<pll> se;
for (int i = 0; i < n; i++) {
ll s, t;
char p, q;
cin >> p >> s >> q >> t;
sum += abs(s-t);
if (p != q) sum++;
else continue;
if (s > t) swap(s, t);
se.push_back(make_pair(s, 0));
se.push_back(make_pair(t, 1));
}
n = (int) se.size()/2;
sort(se.begin(), se.end());
vector<ll> dpe(2*n, 0);
ll ae = 0;
for (int i = 1; i < 2*n; i++) {
dpe[i] = dpe[i-1];
if (se[i].first == se[i-1].first) {
if (se[i].second == 1) ae++;
continue;
}
dpe[i] += 2*(se[i].first-se[i-1].first)*ae;
if (se[i].second == 1) ae++;
}
vector<ll> dps(2*n, 0);
ll as = 0;
for (int i = 2*n-2; i >= 0; i--) {
dps[i] = dps[i+1];
if (se[i].first == se[i+1].first) {
if (se[i].second == 0) as++;
continue;
}
dps[i] += 2*(se[i+1].first-se[i].first)*as;
if (se[i].second == 0) as++;
}
ll mini = numeric_limits<ll>::max()/2;
if (n == 0) mini = 0;
for (int i = 0; i < 2*n; i++) {
mini = min(mini, dps[i]+dpe[i]);
}
cout << sum+mini;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
536 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
19 ms |
7376 KB |
Output is correct |
13 |
Correct |
35 ms |
8900 KB |
Output is correct |
14 |
Correct |
30 ms |
7120 KB |
Output is correct |
15 |
Correct |
20 ms |
5328 KB |
Output is correct |
16 |
Correct |
20 ms |
8136 KB |
Output is correct |
17 |
Correct |
23 ms |
8908 KB |
Output is correct |
18 |
Correct |
27 ms |
8640 KB |
Output is correct |
19 |
Correct |
35 ms |
8904 KB |
Output is correct |
20 |
Correct |
25 ms |
8432 KB |
Output is correct |
21 |
Correct |
31 ms |
8628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |