#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
multiset<ll> st,en;
ll lsum=0,rsum=0;
bool compare(pair<ll,ll> a,pair<ll,ll> b){
return a.f+a.s<b.f+b.s;
}
void insert(ll v){
ll mid=(st.size()?*st.rbegin():1e9+1);
if(v<=mid){
st.insert(v);
lsum+=v;
}
else{
en.insert(v);
rsum+=v;
}
while(st.size()>en.size()+1){
lsum-=*st.rbegin();
rsum+=*st.rbegin();
en.insert(*st.rbegin());
st.erase(st.find(*st.rbegin()));
}
while(en.size()>st.size()){
lsum+=*en.begin();
rsum-=*en.begin();
st.insert(*en.begin());
en.erase(en.find(*en.begin()));
}
}
int main() {_
ll k,n;
cin>>k>>n;
ll ans=0;
vector<pair<ll,ll>> num;
for(ll i=0;i<n;i++){
char a,b;
ll s,t;
cin>>a>>s>>b>>t;
if(a==b) ans+=abs(s-t);
else num.push_back({s,t});
}
sort(num.begin(),num.end(),compare);
vector<ll> pre(num.size()+1,0);
for(ll i=0;i<num.size();i++){
insert(num[i].f);
insert(num[i].s);
pre[i+1]=rsum-lsum;
}
ll ans2=pre[num.size()];
if(k==2){
st.clear();
en.clear();
lsum=0;
rsum=0;
for(ll i=num.size();i>=1;i--){
insert(num[i-1].f);
insert(num[i-1].s);
ans2=min(ans2,rsum-lsum+pre[i-1]);
/*or(auto v=st.begin();v!=st.end();v++){
cout<<*v<<' ';
}
cout<<'\n';
for(auto v=en.begin();v!=en.end();v++){
cout<<*v<<' ';
}
cout<<'\n';*/
}
}
cout<<ans+ans2+num.size();
return 0;
}
//maybe its multiset not set
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:53: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]
53 | for(ll i=0;i<num.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
80 ms |
11940 KB |
Output is correct |
13 |
Correct |
123 ms |
11960 KB |
Output is correct |
14 |
Correct |
100 ms |
10812 KB |
Output is correct |
15 |
Correct |
65 ms |
7208 KB |
Output is correct |
16 |
Correct |
84 ms |
12040 KB |
Output is correct |
17 |
Correct |
84 ms |
11988 KB |
Output is correct |
18 |
Correct |
67 ms |
12036 KB |
Output is correct |
19 |
Correct |
92 ms |
11996 KB |
Output is correct |
20 |
Correct |
90 ms |
12020 KB |
Output is correct |
21 |
Correct |
96 ms |
12068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
448 KB |
Output is correct |
14 |
Correct |
2 ms |
412 KB |
Output is correct |
15 |
Correct |
2 ms |
340 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
360 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
135 ms |
12728 KB |
Output is correct |
26 |
Correct |
233 ms |
12960 KB |
Output is correct |
27 |
Correct |
227 ms |
13688 KB |
Output is correct |
28 |
Correct |
279 ms |
14324 KB |
Output is correct |
29 |
Correct |
234 ms |
14312 KB |
Output is correct |
30 |
Correct |
88 ms |
7744 KB |
Output is correct |
31 |
Correct |
126 ms |
13696 KB |
Output is correct |
32 |
Correct |
143 ms |
14384 KB |
Output is correct |
33 |
Correct |
122 ms |
14104 KB |
Output is correct |
34 |
Correct |
174 ms |
14268 KB |
Output is correct |
35 |
Correct |
142 ms |
13888 KB |
Output is correct |
36 |
Correct |
144 ms |
14020 KB |
Output is correct |
37 |
Correct |
157 ms |
12752 KB |
Output is correct |