#include<bits/stdc++.h>
using namespace std;
long long delta,ans=1ll<<60;
vector<pair<int,int>> a;
struct {
multiset<int> L,R;
long long sumL=0,sumR=0;
long long get(){
return sumR-1ll*R.size()*(*R.begin())+1ll*L.size()*(*R.begin())-sumL;
}
void meow(){
while(L.size()>R.size()+1){
R.insert(*--L.end());
sumR+=*L.begin();
sumL-=*L.begin();
L.erase(--L.end());
}
while(R.size()>L.size()+1){
L.insert(*R.begin());
sumL+=*R.begin();
sumR-=*R.begin();
R.erase(R.begin());
}
while(L.size() && R.size() && *R.begin()<*--L.end()){
int deltaL=*--L.end(),deltaR=*R.begin();
sumL=sumL-deltaL+deltaR;
sumR=sumR-deltaR+deltaL;
L.erase(L.find(deltaL));
R.erase(R.find(deltaR));
L.insert(deltaR);
R.insert(deltaL);
}
}
void insert(const int a){
R.insert(a);
sumR+=a;
meow();
}
void erase(const int a){
if(a<=*--L.end()){
L.erase(L.find(a));
sumL-=a;
}
else{
R.erase(R.find(a));
sumR-=a;
}
meow();
}
} L,R;
int main(){
int k,n; cin>>k>>n;
for(int i=1;i<=n;++i){
char p,q; int s,t; cin>>p>>s>>q>>t;
if(p==q)delta+=abs(s-t);
else{
++delta;
if(t<s)swap(s,t);
a.emplace_back(s,t);
}
}
sort(a.begin(),a.end(),[](const pair<int,int> &a,const pair<int,int> &b){return a.first+a.second<b.first+b.second;});
for(auto &p:a)R.insert(p.first),R.insert(p.second);
if(k==1){
ans=min(ans,R.get());
}
else{
ans=min(ans,R.get());
for(int i=1;i<a.size();++i){
L.insert(a[i-1].first); L.insert(a[i-1].second);
R.erase(a[i-1].first); R.erase(a[i-1].second);
ans=min(ans,L.get()+R.get());
}
}
cout<<ans+delta<<endl;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:73:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<a.size();++i){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
4 ms |
564 KB |
Output is correct |
4 |
Correct |
5 ms |
564 KB |
Output is correct |
5 |
Correct |
4 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
712 KB |
Output is correct |
7 |
Correct |
4 ms |
712 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
6 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
3 |
Correct |
3 ms |
716 KB |
Output is correct |
4 |
Correct |
4 ms |
716 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
4 ms |
772 KB |
Output is correct |
9 |
Correct |
3 ms |
772 KB |
Output is correct |
10 |
Correct |
3 ms |
772 KB |
Output is correct |
11 |
Correct |
4 ms |
788 KB |
Output is correct |
12 |
Correct |
146 ms |
10824 KB |
Output is correct |
13 |
Correct |
361 ms |
10824 KB |
Output is correct |
14 |
Correct |
277 ms |
10824 KB |
Output is correct |
15 |
Correct |
147 ms |
10824 KB |
Output is correct |
16 |
Correct |
211 ms |
10824 KB |
Output is correct |
17 |
Correct |
205 ms |
10896 KB |
Output is correct |
18 |
Correct |
202 ms |
10896 KB |
Output is correct |
19 |
Correct |
219 ms |
10896 KB |
Output is correct |
20 |
Correct |
198 ms |
10896 KB |
Output is correct |
21 |
Correct |
209 ms |
10896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10896 KB |
Output is correct |
2 |
Correct |
3 ms |
10896 KB |
Output is correct |
3 |
Correct |
2 ms |
10896 KB |
Output is correct |
4 |
Correct |
2 ms |
10896 KB |
Output is correct |
5 |
Correct |
2 ms |
10896 KB |
Output is correct |
6 |
Correct |
2 ms |
10896 KB |
Output is correct |
7 |
Correct |
3 ms |
10896 KB |
Output is correct |
8 |
Correct |
3 ms |
10896 KB |
Output is correct |
9 |
Correct |
3 ms |
10896 KB |
Output is correct |
10 |
Correct |
2 ms |
10896 KB |
Output is correct |
11 |
Correct |
3 ms |
10896 KB |
Output is correct |
12 |
Correct |
3 ms |
10896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10896 KB |
Output is correct |
2 |
Correct |
2 ms |
10896 KB |
Output is correct |
3 |
Correct |
2 ms |
10896 KB |
Output is correct |
4 |
Correct |
3 ms |
10896 KB |
Output is correct |
5 |
Correct |
2 ms |
10896 KB |
Output is correct |
6 |
Correct |
2 ms |
10896 KB |
Output is correct |
7 |
Correct |
2 ms |
10896 KB |
Output is correct |
8 |
Correct |
3 ms |
10896 KB |
Output is correct |
9 |
Correct |
2 ms |
10896 KB |
Output is correct |
10 |
Correct |
2 ms |
10896 KB |
Output is correct |
11 |
Correct |
2 ms |
10896 KB |
Output is correct |
12 |
Correct |
2 ms |
10896 KB |
Output is correct |
13 |
Correct |
4 ms |
10896 KB |
Output is correct |
14 |
Correct |
4 ms |
10896 KB |
Output is correct |
15 |
Correct |
4 ms |
10896 KB |
Output is correct |
16 |
Correct |
2 ms |
10896 KB |
Output is correct |
17 |
Correct |
4 ms |
10896 KB |
Output is correct |
18 |
Correct |
3 ms |
10896 KB |
Output is correct |
19 |
Correct |
4 ms |
10896 KB |
Output is correct |
20 |
Correct |
5 ms |
10896 KB |
Output is correct |
21 |
Correct |
4 ms |
10896 KB |
Output is correct |
22 |
Correct |
4 ms |
10896 KB |
Output is correct |
23 |
Correct |
4 ms |
10896 KB |
Output is correct |
24 |
Correct |
4 ms |
10896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10896 KB |
Output is correct |
2 |
Correct |
3 ms |
10896 KB |
Output is correct |
3 |
Correct |
3 ms |
10896 KB |
Output is correct |
4 |
Correct |
3 ms |
10896 KB |
Output is correct |
5 |
Correct |
3 ms |
10896 KB |
Output is correct |
6 |
Correct |
2 ms |
10896 KB |
Output is correct |
7 |
Correct |
2 ms |
10896 KB |
Output is correct |
8 |
Correct |
2 ms |
10896 KB |
Output is correct |
9 |
Correct |
2 ms |
10896 KB |
Output is correct |
10 |
Correct |
2 ms |
10896 KB |
Output is correct |
11 |
Correct |
3 ms |
10896 KB |
Output is correct |
12 |
Correct |
2 ms |
10896 KB |
Output is correct |
13 |
Correct |
3 ms |
10896 KB |
Output is correct |
14 |
Correct |
4 ms |
10896 KB |
Output is correct |
15 |
Correct |
5 ms |
10896 KB |
Output is correct |
16 |
Correct |
3 ms |
10896 KB |
Output is correct |
17 |
Correct |
3 ms |
10896 KB |
Output is correct |
18 |
Correct |
3 ms |
10896 KB |
Output is correct |
19 |
Correct |
3 ms |
10896 KB |
Output is correct |
20 |
Correct |
5 ms |
10896 KB |
Output is correct |
21 |
Correct |
5 ms |
10896 KB |
Output is correct |
22 |
Correct |
5 ms |
10896 KB |
Output is correct |
23 |
Correct |
4 ms |
10896 KB |
Output is correct |
24 |
Correct |
4 ms |
10896 KB |
Output is correct |
25 |
Correct |
285 ms |
10896 KB |
Output is correct |
26 |
Correct |
411 ms |
10896 KB |
Output is correct |
27 |
Correct |
581 ms |
10916 KB |
Output is correct |
28 |
Correct |
643 ms |
10916 KB |
Output is correct |
29 |
Correct |
634 ms |
10940 KB |
Output is correct |
30 |
Correct |
293 ms |
10940 KB |
Output is correct |
31 |
Correct |
268 ms |
10948 KB |
Output is correct |
32 |
Correct |
333 ms |
10948 KB |
Output is correct |
33 |
Correct |
323 ms |
10948 KB |
Output is correct |
34 |
Correct |
384 ms |
10948 KB |
Output is correct |
35 |
Correct |
303 ms |
10948 KB |
Output is correct |
36 |
Correct |
304 ms |
10948 KB |
Output is correct |
37 |
Correct |
241 ms |
10948 KB |
Output is correct |