This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define N 200005
using namespace std;
struct node{
int l,r;
bool operator <(node other){
return l + r < other.l + other.r;
}
};
struct median{
long long suml,sumr;
multiset<int> l,r;
void clear(){
suml = sumr = 0;
l.clear(),r.clear();
}
void addl(int val){
suml += val;
l.insert(val);
}
void addr(int val){
sumr += val;
r.insert(val);
}
void erasel(int val){
suml -= val;
l.erase(l.find(val));
}
void eraser(int val){
sumr -= val;
r.erase(r.find(val));
}
void add(int val){
if(l.size() == 0){
addl(val);
return;
}
if(l.size() == r.size()){
int num1 = min(val,*r.begin());
int num2 = max(val,*r.begin());
addl(num1);
eraser(*r.begin());
addr(num2);
}
else{
int num1 = min(val,*l.rbegin());
int num2 = max(val,*l.rbegin());
addr(num2);
erasel(*l.rbegin());
addl(num1);
}
}
long long get(){
long long val = *l.rbegin();
return (val * l.size() - suml) + (sumr - val * r.size());
}
};
long long pre[N];
long long suf[N];
void solve(){
int k,n;
cin >> k >> n;
long long sum = 0;
vector<node> nodes;
for(int i = 1;i<=n;i++){
int l,r;
char x,y;
cin >> x >> l >> y >> r;
if(x != y){
nodes.push_back({l,r});
}
else sum += abs(l-r);
}
sort(nodes.begin(),nodes.end());
median m;
m.clear();
for(int i=0;i<nodes.size();i++){
m.add(nodes[i].l);
m.add(nodes[i].r);
pre[i+1] = m.get();
}
m.clear();
for(int i=nodes.size()-1;i>=0;i--){
m.add(nodes[i].l);
m.add(nodes[i].r);
suf[i+1] = m.get();
}
long long ans = pre[nodes.size()];
if(k == 2){
for(int i = 1;i<nodes.size();i++){
ans = min(ans, pre[i] + suf[i+1]);
}
}
cout << ans + sum + nodes.size();
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message (stderr)
bridge.cpp: In function 'void solve()':
bridge.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=0;i<nodes.size();i++){
| ~^~~~~~~~~~~~~
bridge.cpp:91:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 1;i<nodes.size();i++){
| ~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |