#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
int k, n;
vector<pair<ll, ll>> a;
ll res;
namespace Sub1{
void solve(){
vector<ll> x;
for(auto [s, t] : a){
x.push_back(s);
x.push_back(t);
}
sort(all(x));
ll pos = x[sz(x) / 2];
for(ll k : x)
res += abs(k - pos);
cout << res + sz(a);
}
}
struct SlidingMedian{
multiset<ll> fi, se;
ll sum_fi = 0, sum_se = 0;
void add(ll x){
se.insert(x);
sum_se += x;
while(sz(fi) < sz(se)){
ll d = *se.begin();
sum_se -= d;
sum_fi += d;
fi.insert(d);
se.erase(se.begin());
}
while(sz(fi) > sz(se)){
ll d = *fi.rbegin();
sum_fi -= d;
sum_se += d;
se.insert(d);
fi.erase(fi.find(d));
}
}
ll get_sum(){
ll med = *se.begin();
return (1ll * sz(fi) * med - sum_fi) + (sum_se - 1ll * sz(se) * med);
}
void reset(){
fi.clear();
se.clear();
sum_fi = sum_se = 0;
}
};
namespace Sub2{
void solve(){
if(sz(a) == 0){
cout << res;
return;
}
sort(all(a), [](pair<ll, ll> &f, pair<ll, ll> &g) -> bool{
return f.F + f.S < g.F + g.S;
});
vector<ll> pre(sz(a)), suf(sz(a));
SlidingMedian st;
for(int i = 0; i < sz(a); ++i){
st.add(a[i].F);
st.add(a[i].S);
pre[i] = st.get_sum();
}
st.reset();
for(int i = sz(a) - 1; i >= 0; --i){
st.add(a[i].F);
st.add(a[i].S);
suf[i] = st.get_sum();
}
ll mn = suf[0];
for(int i = 0; i < sz(a) - 1; ++i)
mn = min(mn, pre[i] + suf[i + 1]);
cout << res + mn + sz(a);
}
}
void solve(){
cin >> k >> n;
for(int i = 1; i <= n; ++i){
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if(p == q)
res += abs(s - t);
else{
if(s > t)
swap(s, t);
a.push_back({s, t});
}
}
if(k == 1)
Sub1::solve();
else
Sub2::solve();
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message
bridge.cpp: In function 'int32_t main()':
bridge.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
480 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
15 ms |
5908 KB |
Output is correct |
13 |
Correct |
31 ms |
7380 KB |
Output is correct |
14 |
Correct |
23 ms |
5968 KB |
Output is correct |
15 |
Correct |
18 ms |
4312 KB |
Output is correct |
16 |
Correct |
18 ms |
6612 KB |
Output is correct |
17 |
Correct |
20 ms |
7220 KB |
Output is correct |
18 |
Correct |
23 ms |
6860 KB |
Output is correct |
19 |
Correct |
30 ms |
7372 KB |
Output is correct |
20 |
Correct |
20 ms |
6824 KB |
Output is correct |
21 |
Correct |
26 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
532 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
516 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
134 ms |
13648 KB |
Output is correct |
26 |
Correct |
203 ms |
13876 KB |
Output is correct |
27 |
Correct |
195 ms |
14788 KB |
Output is correct |
28 |
Correct |
209 ms |
15280 KB |
Output is correct |
29 |
Correct |
203 ms |
15300 KB |
Output is correct |
30 |
Correct |
86 ms |
8136 KB |
Output is correct |
31 |
Correct |
115 ms |
14540 KB |
Output is correct |
32 |
Correct |
129 ms |
15044 KB |
Output is correct |
33 |
Correct |
94 ms |
14732 KB |
Output is correct |
34 |
Correct |
152 ms |
15052 KB |
Output is correct |
35 |
Correct |
127 ms |
14840 KB |
Output is correct |
36 |
Correct |
136 ms |
15048 KB |
Output is correct |
37 |
Correct |
121 ms |
13756 KB |
Output is correct |