#include<bits/stdc++.h>
using namespace std;
#define ii pair<int,int>
#define fi first
#define se second
int k,n;
long long sumL=0,sumU=0;
vector<ii>v;
multiset<int>low;
multiset<int>up;
long long pref[100005];
bool cmp(ii a,ii b)
{
return (a.fi+a.se)<(b.fi+b.se);
}
void capnhat(int x)
{
int val;
if(low.size()==0)val=1000000007;
else val=*low.rbegin();
if(x<=val){
low.insert(x);
sumL+=x;
}
else{
up.insert(x);
sumU+=x;
}
if(low.size()>up.size()+1){
up.insert(*low.rbegin());
sumU+=(*low.rbegin());
sumL-=(*low.rbegin());
low.erase(low.find(*low.rbegin()));
}
else if(up.size()>low.size()){
low.insert(*up.begin());
sumL+=(*up.begin());
sumU-=(*up.begin());
up.erase(up.find(*up.begin()));
}
}
main()
{
cin>>k>>n;
long long delt=0;
for(int i=1;i<=n;i++){
char a,b;
int x,y;
cin>>a>>x>>b>>y;
if(a==b){
delt+=abs(x-y);
}
else{
v.push_back({x,y});
}
}
sort(v.begin(),v.end(),cmp);
long long ans=LLONG_MAX;
for(int i=0;i<v.size();i++){
capnhat(v[i].fi);
capnhat(v[i].se);
pref[i]=sumU-sumL;
}
ans=min(ans,sumU-sumL+(long long)v.size());
if(k==2){
sumL=0,sumU=0;
low.clear();
up.clear();
for(int i=v.size()-1;i>0;i--){
capnhat(v[i].fi);
capnhat(v[i].se);
ans=min(ans,pref[i-1]+sumU-sumL+(long long)v.size());
}
}
cout<<ans+delt;
return 0;
}
Compilation message
bridge.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
42 | main()
| ^~~~
bridge.cpp: In function 'int main()':
bridge.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
71 ms |
11200 KB |
Output is correct |
13 |
Correct |
138 ms |
11208 KB |
Output is correct |
14 |
Correct |
103 ms |
10180 KB |
Output is correct |
15 |
Correct |
72 ms |
6856 KB |
Output is correct |
16 |
Correct |
80 ms |
11236 KB |
Output is correct |
17 |
Correct |
95 ms |
11336 KB |
Output is correct |
18 |
Correct |
79 ms |
11204 KB |
Output is correct |
19 |
Correct |
121 ms |
11208 KB |
Output is correct |
20 |
Correct |
94 ms |
11204 KB |
Output is correct |
21 |
Correct |
98 ms |
11204 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
576 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
568 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
112 ms |
12184 KB |
Output is correct |
26 |
Correct |
168 ms |
12304 KB |
Output is correct |
27 |
Correct |
190 ms |
12864 KB |
Output is correct |
28 |
Correct |
202 ms |
13512 KB |
Output is correct |
29 |
Correct |
205 ms |
13512 KB |
Output is correct |
30 |
Correct |
98 ms |
7324 KB |
Output is correct |
31 |
Correct |
115 ms |
12860 KB |
Output is correct |
32 |
Correct |
155 ms |
13512 KB |
Output is correct |
33 |
Correct |
113 ms |
13268 KB |
Output is correct |
34 |
Correct |
138 ms |
13504 KB |
Output is correct |
35 |
Correct |
136 ms |
12996 KB |
Output is correct |
36 |
Correct |
132 ms |
13252 KB |
Output is correct |
37 |
Correct |
124 ms |
12148 KB |
Output is correct |