#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen(".inp", "r"))
assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
using namespace chrono;
auto start = steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = steady_clock::now();
cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 1e5 + 5;
int n, k;
long long pre[N], suf[N], extra;
vector<pair<int, int>> seg;
struct median_pq {
priority_queue<int> pql, pqr;
long long suml = 0, sumr = 0;
inline void clear() {
suml = sumr = 0;
while (!pql.empty()) pql.pop();
while (!pqr.empty()) pqr.pop();
}
inline void push(int a, int b) {
pql.push(+a), suml += a;
pqr.push(-b), sumr += b;
if (pql.top() > - pqr.top()) {
a = pql.top();
b = pqr.top();
pql.pop();
pqr.pop();
pql.push(-b);
pqr.push(-a);
suml = suml - a - b;
sumr = sumr + a + b;
}
}
inline long long get() {
return sumr - suml;
}
} pq;
void Hollwo_Pelw(){
cin >> k >> n;
for (int i = 0; i < n; i++) {
char ta, tb; int pa, pb;
cin >> ta >> pa >> tb >> pb;
if (pa > pb) swap(pa, pb);
if (ta == tb) {
extra += abs(pa - pb);
}
else {
seg.emplace_back(pa, pb);
}
}
n = seg.size(), extra += n;
sort(seg.begin(), seg.end(), [&](const pair<int, int> &a, const pair<int, int> &b){
return a.first + a.second < b.first + b.second;
});
pq.clear();
for (int i = 0; i < n; i++) {
pq.push(seg[i].first, seg[i].second);
pre[i + 1] = pq.get();
// cout << "add " << seg[i].first << ' ' << seg[i].second << '\n' << pq.get() << '\n';
}
pq.clear();
for (int i = n; i --; ) {
pq.push(seg[i].first, seg[i].second);
suf[i + 1] = pq.get();
// cout << "add " << seg[i].first << ' ' << seg[i].second << '\n' << pq.get() << '\n';
}
long long res = pre[n];
if (k == 1) {
cout << res + extra << '\n';
return ;
}
for (int i = 1; i <= n; i++)
res = min(res, pre[i - 1] + suf[i]);
cout << res + extra << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
340 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 |
32 ms |
4636 KB |
Output is correct |
13 |
Correct |
90 ms |
6240 KB |
Output is correct |
14 |
Correct |
71 ms |
4800 KB |
Output is correct |
15 |
Correct |
57 ms |
3840 KB |
Output is correct |
16 |
Correct |
44 ms |
5572 KB |
Output is correct |
17 |
Correct |
77 ms |
6224 KB |
Output is correct |
18 |
Correct |
45 ms |
5832 KB |
Output is correct |
19 |
Correct |
94 ms |
6232 KB |
Output is correct |
20 |
Correct |
64 ms |
5788 KB |
Output is correct |
21 |
Correct |
91 ms |
5988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 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 |
320 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
328 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 |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
400 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 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 |
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 |
356 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
32 ms |
4680 KB |
Output is correct |
26 |
Correct |
64 ms |
4816 KB |
Output is correct |
27 |
Correct |
97 ms |
5620 KB |
Output is correct |
28 |
Correct |
105 ms |
6228 KB |
Output is correct |
29 |
Correct |
91 ms |
6228 KB |
Output is correct |
30 |
Correct |
44 ms |
3528 KB |
Output is correct |
31 |
Correct |
36 ms |
5500 KB |
Output is correct |
32 |
Correct |
82 ms |
6240 KB |
Output is correct |
33 |
Correct |
49 ms |
5852 KB |
Output is correct |
34 |
Correct |
93 ms |
6236 KB |
Output is correct |
35 |
Correct |
51 ms |
5800 KB |
Output is correct |
36 |
Correct |
87 ms |
5952 KB |
Output is correct |
37 |
Correct |
26 ms |
4656 KB |
Output is correct |