#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using I = int;
using C = char;
using B = bool;
using Lli = long long int;
vector<pair<I, I>> pais;
vector<I> pois;
vector<pair<I, pair<I, I>>> mids;
multiset<I> low_lows;
multiset<I> low_upps;
multiset<I> upp_lows;
multiset<I> upp_upps;
Lli dis = 0;
Lli slv1() {
if (pais.empty())
return 0;
for (const auto [s, t] : pais) {
pois.push_back(s);
pois.push_back(t);
}
const I mid = pois.size() / 2;
nth_element(pois.begin(), pois.begin() + mid, pois.end());
const I med = pois[mid];
Lli res = 0;
for (const auto poi : pois)
res += abs(med - poi);
return res;
}
void upp_bal() {
if (upp_lows.empty() && !upp_upps.empty()) {
const I mov = *upp_upps.begin();
upp_lows.insert(mov);
upp_upps.erase(upp_upps.find(mov));
return;
}
if (upp_upps.size() > upp_lows.size()) {
const I las = *upp_lows.rbegin();
const I mov = *upp_upps.begin();
upp_lows.insert(mov);
upp_upps.erase(upp_upps.find(mov));
const I cur = *upp_lows.rbegin();
dis += (upp_lows.size() - upp_upps.size() - 2) * (cur - las);
}
if (upp_lows.size() > upp_upps.size() + 1) {
const I las = *upp_lows.rbegin();
const I mov = *upp_lows.rbegin();
upp_upps.insert(mov);
upp_lows.erase(upp_lows.find(mov));
const I cur = *upp_lows.rbegin();
dis -= (upp_upps.size() - upp_lows.size()) * (las - cur);
}
}
void low_bal() {
if (low_upps.size() > low_lows.size()) {
const I las = *low_lows.rbegin();
const I mov = *low_upps.begin();
low_lows.insert(mov);
low_upps.erase(low_upps.find(mov));
const I cur = *low_lows.rbegin();
dis += (low_lows.size() - low_upps.size() - 2) * (cur - las);
}
if (low_lows.size() > low_upps.size() + 1) {
const I las = *low_lows.rbegin();
const I mov = *low_lows.rbegin();
low_upps.insert(mov);
low_lows.erase(low_lows.find(mov));
const I cur = *low_lows.rbegin();
dis -= (low_upps.size() - low_lows.size()) * (las - cur);
}
}
void upp_add(I val) {
if (upp_lows.empty()) {
upp_lows.insert(val);
return;
}
const I med = *upp_lows.rbegin();
if (val > med) {
upp_upps.insert(val);
dis += val - med;
} else {
upp_lows.insert(val);
dis += med - val;
}
upp_bal();
}
void low_add(I val) {
if (low_lows.empty()) {
low_lows.insert(val);
return;
}
const I med = *low_lows.rbegin();
if (val > med) {
low_upps.insert(val);
dis += val - med;
} else {
low_lows.insert(val);
dis += med - val;
}
low_bal();
}
void upp_rem(I val) {
const I med = *upp_lows.rbegin();
if (val > med) {
upp_upps.erase(upp_upps.find(val));
dis -= val - med;
} else {
upp_lows.erase(upp_lows.find(val));
dis -= med - val;
}
upp_bal();
}
Lli slv2() {
if (pais.empty())
return 0;
for (const auto [s, t] : pais) {
upp_add(s);
upp_add(t);
}
for (const auto [s, t] : pais)
mids.push_back({s + (t - s) / 2, {s, t}});
sort(mids.begin(), mids.end());
Lli res = dis;
for (const auto [mid, pai] : mids) {
const auto [s, t] = pai;
upp_rem(s);
upp_rem(t);
low_add(s);
low_add(t);
res = min(res, dis);
}
return res;
}
I main(void) {
cin.tie(0)->sync_with_stdio(0);
I k, n;
cin >> k >> n;
Lli res = 0;
for (I i = 0; i < n; i++) {
C p, q;
I s, t;
cin >> p >> s >> q >> t;
if (s > t)
swap(s, t);
if (p != q) {
pais.push_back({s, t});
res++;
} else
res += t - s;
}
if (k == 1)
res += slv1();
else
res += slv2();
printf("%lli\n", res);
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
348 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 |
# |
Verdict |
Execution time |
Memory |
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 |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
19 ms |
3380 KB |
Output is correct |
13 |
Correct |
30 ms |
4936 KB |
Output is correct |
14 |
Correct |
19 ms |
3728 KB |
Output is correct |
15 |
Correct |
17 ms |
2832 KB |
Output is correct |
16 |
Correct |
24 ms |
4296 KB |
Output is correct |
17 |
Correct |
25 ms |
4884 KB |
Output is correct |
18 |
Correct |
24 ms |
4520 KB |
Output is correct |
19 |
Correct |
26 ms |
4880 KB |
Output is correct |
20 |
Correct |
23 ms |
4432 KB |
Output is correct |
21 |
Correct |
32 ms |
4668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
2 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 |
388 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
340 KB |
Output is correct |
25 |
Correct |
172 ms |
12812 KB |
Output is correct |
26 |
Correct |
314 ms |
12748 KB |
Output is correct |
27 |
Correct |
369 ms |
12740 KB |
Output is correct |
28 |
Correct |
407 ms |
12708 KB |
Output is correct |
29 |
Correct |
381 ms |
12784 KB |
Output is correct |
30 |
Correct |
208 ms |
6852 KB |
Output is correct |
31 |
Correct |
172 ms |
12704 KB |
Output is correct |
32 |
Correct |
229 ms |
12804 KB |
Output is correct |
33 |
Correct |
241 ms |
12764 KB |
Output is correct |
34 |
Correct |
257 ms |
12712 KB |
Output is correct |
35 |
Correct |
185 ms |
12720 KB |
Output is correct |
36 |
Correct |
202 ms |
12684 KB |
Output is correct |
37 |
Correct |
214 ms |
12684 KB |
Output is correct |