#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define int long long
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<ll, ll>;
struct GetMedian{
multiset<ll> fi;
multiset<ll> se;
ll fsum=0, ssum=0;
void add(ll x){
se.insert(x);
ssum+=x;
while(se.size() && se.size() > fi.size()){
ll val=*se.begin();
ssum-=val;
se.erase(se.begin());
fi.insert(val);
fsum+=val;
}
while(fi.size() && fi.size() > se.size()){
ll val=*fi.rbegin();
fsum-=val;
fi.erase(fi.find(val));
se.insert(val);
ssum+=val;
}
}
ll get(){
ll med=*se.begin();
ll val=(med * fi.size() - fsum) + (ssum - med * se.size());
return val;
}
void clear(){
fi.clear();
se.clear();
fsum=ssum=0;
}
};
int k, n;
const int N=1e5+13;
int a[N];
ll res;
char c1, c2;
int x, y;
namespace sub1{
vector<int> v;
void solve(){
for(int i=1; i<=n; i++){
cin >> c1 >> x >> c2 >> y;
if(c1==c2){
res+=abs(x-y);
}
else{
v.push_back(x);
v.push_back(y);
++res;
}
}
sort(begin(v), end(v));
for(int c: v){
res+=abs(c-v[v.size()/2]);
}
cout << res;
}
}
vector<pair<int, int>> v;
ll l[N];
ll r[N];
void solve(){
cin >> k >> n;
if(k==1){
sub1::solve();
exit(0);
}
v.push_back({0, 0});
for(int i=1; i<=n; i++){
cin >> c1 >> x >> c2 >> y;
if(c1==c2){
res+=abs(x-y);
}
else{
if(x > y) swap(x, y);
v.push_back(make_pair(x, y));
++res;
}
}
if(v.empty()){
cout << res;
return;
}
sort(begin(v)+1, end(v), [&](pii x, pii y){
return x.fi + x.se < y.fi + y.se;
});
n=v.size()-1;
GetMedian sus;
for(int i=1; i<=n; i++){
sus.add(v[i].fi);
sus.add(v[i].se);
l[i]=sus.get();
}
sus.clear();
for(int i=n; i>=1; i--){
sus.add(v[i].fi);
sus.add(v[i].se);
r[i]=sus.get();
}
ll ans=l[n];
for(int i=1; i<n; i++){
ans=min(ans, l[i]+r[i+1]);
}
cout << res+ans;
}
main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
Compilation message
bridge.cpp:131:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
131 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
14 ms |
4512 KB |
Output is correct |
13 |
Correct |
31 ms |
4556 KB |
Output is correct |
14 |
Correct |
20 ms |
4568 KB |
Output is correct |
15 |
Correct |
16 ms |
3548 KB |
Output is correct |
16 |
Correct |
16 ms |
4520 KB |
Output is correct |
17 |
Correct |
21 ms |
4628 KB |
Output is correct |
18 |
Correct |
23 ms |
4564 KB |
Output is correct |
19 |
Correct |
27 ms |
4564 KB |
Output is correct |
20 |
Correct |
24 ms |
4568 KB |
Output is correct |
21 |
Correct |
25 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2436 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2660 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2568 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2488 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2904 KB |
Output is correct |
23 |
Correct |
2 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
140 ms |
14284 KB |
Output is correct |
26 |
Correct |
191 ms |
14536 KB |
Output is correct |
27 |
Correct |
184 ms |
15304 KB |
Output is correct |
28 |
Correct |
191 ms |
15704 KB |
Output is correct |
29 |
Correct |
186 ms |
15952 KB |
Output is correct |
30 |
Correct |
93 ms |
9676 KB |
Output is correct |
31 |
Correct |
116 ms |
15048 KB |
Output is correct |
32 |
Correct |
141 ms |
15816 KB |
Output is correct |
33 |
Correct |
90 ms |
15556 KB |
Output is correct |
34 |
Correct |
142 ms |
15812 KB |
Output is correct |
35 |
Correct |
130 ms |
15300 KB |
Output is correct |
36 |
Correct |
137 ms |
15520 KB |
Output is correct |
37 |
Correct |
127 ms |
14280 KB |
Output is correct |