#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
int K;int n;
int obase;
pair<int,int>ar[100005];
void init(){
cin>>K>>n;
obase=0;
vector<pair<int,int>>vc;
for(int i=1;i<=n;i++){
char a;char b;
int pa;int pb;
cin>>a>>pa>>b>>pb;
if(a==b){
obase+=abs(pa-pb);
}else{
obase++;
vc.push_back({min(pa,pb),max(pa,pb)});
}
}
n=vc.size();
for(int i=0;i<vc.size();i++){
ar[i+1]=vc[i];
}
sort(ar+1,ar+n+1);
if(n==0){
cout<<obase;exit(0);
}
}
bool cmp(pair<int,int>i,pair<int,int>j){
if(i.first+i.second<j.first+j.second){return 1;}
return 0;
}
void solvek1(){
vector<int>vc;
for(int i=1;i<=n;i++){
vc.push_back(ar[i].first);
vc.push_back(ar[i].second);
}
sort(vc.begin(),vc.end());
int ans=0;
for(int i=0;i<vc.size()/2;i++){
ans+=vc[vc.size()-i-1]-vc[i];
}
cout<<ans+obase<<endl;
}
int ps[200005];
int sf[200005];
multiset<int>mst1;
multiset<int>mst2;
int tot;
void ins(int v){
if(mst2.size()==0||(*mst2.begin())<=v){
tot+=v;
mst2.insert(v);
}else{
tot-=v;
mst1.insert(v);
}
while(mst2.size()>mst1.size()){
v=(*mst2.begin());
tot-=v;
tot-=v;
mst2.erase(mst2.begin());
mst1.insert(v);
}
while(mst1.size()>mst2.size()){
v=(*mst1.rbegin());
tot+=v;
tot+=v;
mst1.erase(mst1.find(v));
mst2.insert(v);
}
}
void solvek2(){
int ans=1e18;
vector<int>vc;
for(int i=1;i<=n;i++){
ps[i]=1e18;
sf[i]=1e18;
}
sort(ar+1,ar+n+1,cmp);
tot=0;
for(int i=1;i<=n;i++){
ins(ar[i].first);
ins(ar[i].second);
ps[i]=tot;
}
tot=0;
mst1.clear();mst2.clear();
for(int i=n;i>=1;i--){
ins(ar[i].first);
ins(ar[i].second);
sf[i]=tot;
}
for(int i=0;i<=n;i++){
ans=min(ans,ps[i]+sf[i+1]);
}
cout<<ans+obase<<endl;
}
signed main(){
init();
if(K==1){solvek1();}else{
solvek2();
}
}
Compilation message
bridge.cpp: In function 'void init()':
bridge.cpp:24:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<vc.size();i++){
| ~^~~~~~~~~~
bridge.cpp: In function 'void solvek1()':
bridge.cpp:44:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<vc.size()/2;i++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2520 KB |
Output is correct |
5 |
Correct |
2 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
39 ms |
6804 KB |
Output is correct |
13 |
Correct |
84 ms |
6308 KB |
Output is correct |
14 |
Correct |
58 ms |
6332 KB |
Output is correct |
15 |
Correct |
48 ms |
4396 KB |
Output is correct |
16 |
Correct |
55 ms |
6464 KB |
Output is correct |
17 |
Correct |
68 ms |
6528 KB |
Output is correct |
18 |
Correct |
65 ms |
6312 KB |
Output is correct |
19 |
Correct |
74 ms |
6308 KB |
Output is correct |
20 |
Correct |
60 ms |
6428 KB |
Output is correct |
21 |
Correct |
67 ms |
6308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4696 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4644 KB |
Output is correct |
23 |
Correct |
2 ms |
4440 KB |
Output is correct |
24 |
Correct |
2 ms |
4440 KB |
Output is correct |
25 |
Correct |
143 ms |
14344 KB |
Output is correct |
26 |
Correct |
188 ms |
15528 KB |
Output is correct |
27 |
Correct |
219 ms |
16016 KB |
Output is correct |
28 |
Correct |
218 ms |
16784 KB |
Output is correct |
29 |
Correct |
226 ms |
16788 KB |
Output is correct |
30 |
Correct |
106 ms |
10744 KB |
Output is correct |
31 |
Correct |
133 ms |
15944 KB |
Output is correct |
32 |
Correct |
169 ms |
17036 KB |
Output is correct |
33 |
Correct |
126 ms |
16280 KB |
Output is correct |
34 |
Correct |
163 ms |
16792 KB |
Output is correct |
35 |
Correct |
162 ms |
16336 KB |
Output is correct |
36 |
Correct |
166 ms |
16536 KB |
Output is correct |
37 |
Correct |
140 ms |
15248 KB |
Output is correct |