#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 18
#define MAXN 200005
vector<pair<ll,ll>> seq;
int main() {
fast
ll K, N;
cin >> K >> N;
ll sum = 0;
for (int i = 0; i < N; i++) {
char ch1, ch2;
ll posR1, posR2;
cin >> ch1 >> posR1 >> ch2 >> posR2;
if (ch1 == ch2)
sum += abs(posR1 - posR2);
else
seq.push_back({min(posR1, posR2), max(posR1, posR2)});
}
sort(seq.begin(), seq.end(), [&](pair<ll,ll> &a, pair<ll,ll> &b) {
return a.f + a.s < b.f + b.s;
});
sum += seq.size();
int n = seq.size();
if (seq.size() == 0) {
cout << sum << "\n";
return 0;
}
ll ans = 1e15;
vector<ll> pref(n+1, 0);
multiset<int> s1, s2;
ll sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++) {
s1.insert(seq[i].f);
s2.insert(seq[i].s);
sum1 += seq[i].f;
sum2 += seq[i].s;
while (*(s2.begin()) < *(--s1.end())) {
s1.insert(*s2.begin());
sum1 += *s2.begin();
sum2 -= *s2.begin();
s2.erase(s2.begin());
s2.insert(*(--s1.end()));
sum1 -= *(--s1.end());
sum2 += *(--s1.end());
s1.erase(s1.find(*(--s1.end())));
}
pref[i] = sum2 - sum1;
}
ans = pref[n-1];
s1 = multiset<int>();
s2 = multiset<int>();
sum1 = sum2 = 0;
if (K == 2) {
for (int i = n-1; i >= 1; i--) {
s1.insert(seq[i].f);
s2.insert(seq[i].s);
sum1 += seq[i].f;
sum2 += seq[i].s;
while (*(s2.begin()) < *(--s1.end())) {
s1.insert(*s2.begin());
sum1 += *s2.begin();
sum2 -= *s2.begin();
s2.erase(s2.begin());
s2.insert(*(--s1.end()));
sum1 -= *(--s1.end());
sum2 += *(--s1.end());
s1.erase(s1.find(*(--s1.end())));
}
ll now = sum2 - sum1;
ans = min(ans, now + pref[i-1]);
}
}
cout << sum + ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
58 ms |
12468 KB |
Output is correct |
13 |
Correct |
100 ms |
12504 KB |
Output is correct |
14 |
Correct |
106 ms |
11324 KB |
Output is correct |
15 |
Correct |
57 ms |
7640 KB |
Output is correct |
16 |
Correct |
53 ms |
12516 KB |
Output is correct |
17 |
Correct |
85 ms |
12480 KB |
Output is correct |
18 |
Correct |
54 ms |
12520 KB |
Output is correct |
19 |
Correct |
87 ms |
12460 KB |
Output is correct |
20 |
Correct |
88 ms |
12500 KB |
Output is correct |
21 |
Correct |
102 ms |
12468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
260 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
360 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
98 ms |
12704 KB |
Output is correct |
26 |
Correct |
212 ms |
12600 KB |
Output is correct |
27 |
Correct |
226 ms |
12472 KB |
Output is correct |
28 |
Correct |
250 ms |
12460 KB |
Output is correct |
29 |
Correct |
230 ms |
12444 KB |
Output is correct |
30 |
Correct |
92 ms |
7040 KB |
Output is correct |
31 |
Correct |
77 ms |
12476 KB |
Output is correct |
32 |
Correct |
173 ms |
12492 KB |
Output is correct |
33 |
Correct |
74 ms |
12496 KB |
Output is correct |
34 |
Correct |
149 ms |
12552 KB |
Output is correct |
35 |
Correct |
141 ms |
12448 KB |
Output is correct |
36 |
Correct |
149 ms |
12500 KB |
Output is correct |
37 |
Correct |
82 ms |
12556 KB |
Output is correct |