#include <bits/stdc++.h>
#define N 200005
using namespace std;
struct node{
int l,r;
bool operator <(node other){
return l + r < other.l + other.r;
}
};
struct median{
long long suml,sumr;
multiset<int> l,r;
void clear(){
suml = sumr = 0;
l.clear(),r.clear();
}
void addl(int val){
suml += val;
l.insert(val);
}
void addr(int val){
sumr += val;
r.insert(val);
}
void erasel(int val){
suml -= val;
l.erase(l.find(val));
}
void eraser(int val){
sumr -= val;
r.erase(r.find(val));
}
void add(int val){
if(l.size() == 0){
addl(val);
return;
}
if(l.size() == r.size()){
int num1 = min(val,*r.begin());
int num2 = max(val,*r.begin());
addl(num1);
eraser(*r.begin());
addr(num2);
}
else{
int num1 = min(val,*l.rbegin());
int num2 = max(val,*l.rbegin());
addr(num2);
erasel(*l.rbegin());
addl(num1);
}
}
long long get(){
long long val = *l.rbegin();
return (val * l.size() - suml) + (sumr - val * r.size());
}
};
long long pre[N];
long long suf[N];
void solve(){
int k,n;
cin >> k >> n;
long long sum = 0;
vector<node> nodes;
for(int i = 1;i<=n;i++){
int l,r;
char x,y;
cin >> x >> l >> y >> r;
if(x != y){
nodes.push_back({l,r});
}
else sum += abs(l-r);
}
sort(nodes.begin(),nodes.end());
median m;
m.clear();
for(int i=0;i<nodes.size();i++){
m.add(nodes[i].l);
m.add(nodes[i].r);
pre[i+1] = m.get();
}
m.clear();
for(int i=nodes.size()-1;i>=0;i--){
m.add(nodes[i].l);
m.add(nodes[i].r);
suf[i+1] = m.get();
}
long long ans = pre[nodes.size()];
if(k == 2){
for(int i = 1;i<nodes.size();i++){
ans = min(ans, pre[i] + suf[i+1]);
}
}
cout << ans + sum + nodes.size();
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=0;i<nodes.size();i++){
| ~^~~~~~~~~~~~~
bridge.cpp:91:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 1;i<nodes.size();i++){
| ~^~~~~~~~~~~~~
# |
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 |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
360 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
352 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 |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
464 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
196 ms |
12768 KB |
Output is correct |
13 |
Correct |
341 ms |
14380 KB |
Output is correct |
14 |
Correct |
306 ms |
12008 KB |
Output is correct |
15 |
Correct |
159 ms |
8516 KB |
Output is correct |
16 |
Correct |
189 ms |
13704 KB |
Output is correct |
17 |
Correct |
205 ms |
14364 KB |
Output is correct |
18 |
Correct |
149 ms |
13992 KB |
Output is correct |
19 |
Correct |
210 ms |
14364 KB |
Output is correct |
20 |
Correct |
213 ms |
13872 KB |
Output is correct |
21 |
Correct |
210 ms |
14228 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 |
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 |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
324 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 |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
468 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 |
2 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
352 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
352 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 |
324 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 |
212 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 |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
472 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
356 KB |
Output is correct |
25 |
Correct |
209 ms |
12816 KB |
Output is correct |
26 |
Correct |
290 ms |
12972 KB |
Output is correct |
27 |
Correct |
350 ms |
13768 KB |
Output is correct |
28 |
Correct |
296 ms |
14312 KB |
Output is correct |
29 |
Correct |
348 ms |
14392 KB |
Output is correct |
30 |
Correct |
129 ms |
7668 KB |
Output is correct |
31 |
Correct |
190 ms |
13632 KB |
Output is correct |
32 |
Correct |
200 ms |
14316 KB |
Output is correct |
33 |
Correct |
155 ms |
13960 KB |
Output is correct |
34 |
Correct |
203 ms |
14284 KB |
Output is correct |
35 |
Correct |
202 ms |
13976 KB |
Output is correct |
36 |
Correct |
210 ms |
14044 KB |
Output is correct |
37 |
Correct |
180 ms |
12744 KB |
Output is correct |