#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.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;
for(int x:low)low.erase(x);
for(int x:up)up.erase(x);
for(int i=v.size();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 |
0 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 |
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 |
344 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 |
1 ms |
392 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 |
12 |
Correct |
70 ms |
11284 KB |
Output is correct |
13 |
Correct |
125 ms |
11224 KB |
Output is correct |
14 |
Correct |
105 ms |
10176 KB |
Output is correct |
15 |
Correct |
70 ms |
6856 KB |
Output is correct |
16 |
Correct |
78 ms |
11364 KB |
Output is correct |
17 |
Correct |
105 ms |
11240 KB |
Output is correct |
18 |
Correct |
80 ms |
11208 KB |
Output is correct |
19 |
Correct |
107 ms |
11200 KB |
Output is correct |
20 |
Correct |
93 ms |
11192 KB |
Output is correct |
21 |
Correct |
99 ms |
11208 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 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Runtime error |
0 ms |
348 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Runtime error |
0 ms |
348 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |