#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); it++)
#define rep(i, N) for(int i = 0; i < (N); i++)
#define rep1(i, N) for(int i = 1; i <= (N); i++)
#define rep2(i, s, e) for(int i = (s); i <= (e); i++)
#define rep3(i, s, e, d) for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#define pb push_back
#ifdef DEBUG
#define debug(x...) { dbg::depth++; string dbg_vals = dbg::to_string(x); dbg::depth--; dbg::fprint(__func__, __LINE__, #x, dbg_vals); }
#define light_debug(x) { dbg::light = 1; dbg::dout << __func__ << ":" << __LINE__ << " " << #x << " = " << x << endl; dbg::light = 0; }
#else
#define debug(x...)
#define light_debug(x)
#endif
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
class median_find {
multiset<int> L, R;
ll sum_l{0}, sum_r{0};
void balance() {
if(L.size() > R.size() + 1) {
int x = *L.rbegin();
sum_l -= x, sum_r += x;
R.insert(x), L.erase(prev(L.end()));
} else if(R.size() > L.size()) {
int x = *R.begin();
sum_r -= x, sum_l += x;
R.erase(R.begin()), L.insert(x);
}
}
public:
void add(int x) {
if(L.empty() || x <= *L.rbegin())
L.insert(x), sum_l += x;
else
R.insert(x), sum_r += x;
balance();
}
void erase(int x) {
auto it = L.find(x);
if(it != L.end())
L.erase(it), sum_l -= x;
else
R.erase(R.find(x)), sum_r -= x;
balance();
}
ll query() const {
ll l = L.size(), r = R.size(), m = L.empty() ? 0 : *L.rbegin();
return sum_r - r * m + l * m - sum_l;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
#ifdef DEBUG
freopen("debug", "w", stderr);
#endif
int K, N; cin >> K >> N;
if(K == 1){
ll ans{};
vi A;
rep(_, N) {
char S, E;
int x, y;
cin >> S >> x >> E >> y;
if(S == E) ans += abs(x - y);
else A.pb(x), A.pb(y), ++ans;
}
debug(ans, A);
sort(all(A));
int m = A[A.size() >> 1];
for(auto x : A)
ans += abs(x - m);
cout << ans << '\n';
} else {
median_find front, back;
ll ans{};
vector<pii> A;
rep(_, N) {
char S, E;
int x, y;
cin >> S >> x >> E >> y;
if(S == E) ans += abs(x - y);
else A.pb({x, y}), ++ans;
}
sort(all(A), [](pii a, pii b) { return a.first + a.second < b.first + b.second; });
N = A.size();
rep(i, N) back.add(A[i].first), back.add(A[i].second);
ll t = back.query();
rep(i, N) {
front.add(A[i].first), front.add(A[i].second);
back.erase(A[i].first), back.erase(A[i].second);
ckmin(t, front.query() + back.query());
}
cout << (ans += t) << '\n';
}
#ifdef DEBUG
dbg::dout << "\nExecution time: " << clock() << "ms\n";
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
23 ms |
1532 KB |
Output is correct |
13 |
Correct |
45 ms |
1524 KB |
Output is correct |
14 |
Correct |
35 ms |
1532 KB |
Output is correct |
15 |
Correct |
26 ms |
1024 KB |
Output is correct |
16 |
Correct |
32 ms |
1532 KB |
Output is correct |
17 |
Correct |
32 ms |
1532 KB |
Output is correct |
18 |
Correct |
41 ms |
1532 KB |
Output is correct |
19 |
Correct |
43 ms |
1500 KB |
Output is correct |
20 |
Correct |
31 ms |
1532 KB |
Output is correct |
21 |
Correct |
40 ms |
1532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
182 ms |
11248 KB |
Output is correct |
26 |
Correct |
338 ms |
11500 KB |
Output is correct |
27 |
Correct |
518 ms |
11664 KB |
Output is correct |
28 |
Correct |
556 ms |
11632 KB |
Output is correct |
29 |
Correct |
539 ms |
12784 KB |
Output is correct |
30 |
Correct |
190 ms |
7028 KB |
Output is correct |
31 |
Correct |
190 ms |
12200 KB |
Output is correct |
32 |
Correct |
195 ms |
12788 KB |
Output is correct |
33 |
Correct |
154 ms |
12400 KB |
Output is correct |
34 |
Correct |
195 ms |
12780 KB |
Output is correct |
35 |
Correct |
189 ms |
12400 KB |
Output is correct |
36 |
Correct |
192 ms |
12528 KB |
Output is correct |
37 |
Correct |
168 ms |
11252 KB |
Output is correct |