#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct Median {
priority_queue<int> lower;
priority_queue<int, vector<int>, greater<int>> upper;
int sumLow, sumUp;
Median() : sumLow(0), sumUp(0) {}
void add(int x) {
if (!lower.empty() and lower.top() < x)
upper.push(x), sumUp += x;
else
lower.push(x), sumLow += x;
while (upper.size() > lower.size()) {
sumUp -= upper.top();
sumLow += upper.top();
lower.push(upper.top());
upper.pop();
}
while (lower.size() > upper.size() + 1) {
sumLow -= lower.top();
sumUp += lower.top();
upper.push(lower.top());
lower.pop();
}
}
int med() {
if (lower.empty())
return 0;
int x = lower.top();
return x * lower.size() - sumLow + sumUp - x * upper.size();
}
};
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int K, N;
cin >> K >> N;
vector<pair<int, int>> intervals;
int sol = 0;
for (int i = 0; i < N; ++i) {
char c, d;
int a, b;
cin >> c >> a >> d >> b;
if (a > b)
swap(a, b);
if (c == d) {
sol += b - a;
continue;
}
sol++;
intervals.emplace_back(a, b);
}
if (K == 1) {
Median med;
for (auto [a, b] : intervals) {
med.add(a);
med.add(b);
}
dbg(sol, med.med());
cout << sol + med.med() << endl;
return 0;
}
sort(intervals.begin(), intervals.end(),
[&](auto l, auto r) { return l.first + l.second < r.first + r.second; });
Median prefMed, suffMed;
vector<int> valPref(intervals.size()), valSuf(intervals.size());
for (int i = 0; i < (int)intervals.size(); ++i) {
prefMed.add(intervals[i].first);
prefMed.add(intervals[i].second);
valPref[i] = prefMed.med();
}
for (int i = intervals.size() - 1; i >= 0; --i) {
suffMed.add(intervals[i].first);
suffMed.add(intervals[i].second);
valSuf[i] = suffMed.med();
}
dbg(valPref);
dbg(valSuf);
int best = intervals.empty() ? 0 : valPref.back();
for (int i = 0; i < (int)intervals.size() - 1; ++i)
best = min(best, valPref[i] + valSuf[i + 1]);
cout << best + sol << endl;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
384 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 |
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 |
12 |
Correct |
17 ms |
4784 KB |
Output is correct |
13 |
Correct |
38 ms |
4780 KB |
Output is correct |
14 |
Correct |
23 ms |
4428 KB |
Output is correct |
15 |
Correct |
17 ms |
2868 KB |
Output is correct |
16 |
Correct |
18 ms |
4712 KB |
Output is correct |
17 |
Correct |
33 ms |
4824 KB |
Output is correct |
18 |
Correct |
30 ms |
4840 KB |
Output is correct |
19 |
Correct |
36 ms |
5244 KB |
Output is correct |
20 |
Correct |
28 ms |
4812 KB |
Output is correct |
21 |
Correct |
32 ms |
4764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
316 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
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 |
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 |
356 KB |
Output is correct |
24 |
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 |
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 |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
11 |
Correct |
0 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 |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
27 ms |
8660 KB |
Output is correct |
26 |
Correct |
38 ms |
9000 KB |
Output is correct |
27 |
Correct |
46 ms |
9356 KB |
Output is correct |
28 |
Correct |
49 ms |
9828 KB |
Output is correct |
29 |
Correct |
49 ms |
10060 KB |
Output is correct |
30 |
Correct |
27 ms |
5328 KB |
Output is correct |
31 |
Correct |
23 ms |
9608 KB |
Output is correct |
32 |
Correct |
42 ms |
10192 KB |
Output is correct |
33 |
Correct |
26 ms |
9732 KB |
Output is correct |
34 |
Correct |
52 ms |
10372 KB |
Output is correct |
35 |
Correct |
35 ms |
9740 KB |
Output is correct |
36 |
Correct |
45 ms |
10024 KB |
Output is correct |
37 |
Correct |
21 ms |
9168 KB |
Output is correct |