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<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
#define int long long
#define ss second
#define ff first
#define endl '\n'
#define ordered_set tree<pair<int, int>, null_type, less_equal<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update>
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k,n; cin>>k>>n;
int sum=0;
vector<pair<int,int> > v,d;
for(int i=0,j=0; i<n; i++){
int s,t;
char p,q;
cin>>p>>s>>q>>t;
if(p==q){
sum+=abs(s-t);
}else{
sum++;
d.push_back({s,t});
v.push_back({s+t,j});
j++;
}
}
if(v.empty()){
cout<<sum;
return 0;
}
int sl=0,sr=0;
ordered_set f,s;
sort(v.begin(),v.end());
for(int i=0; i<v.size(); i++){
s.insert({d[v[i].ss].ff,v[i].ss});
s.insert({d[v[i].ss].ss,v[i].ss});
}
int ml=(*s.find_by_order(1)).ff;
int mr=(*s.find_by_order(s.size()/2)).ff;
for(int i=0; i<v.size(); i++){
sr+=abs(mr-d[v[i].ss].ff);
sr+=abs(mr-d[v[i].ss].ss);
}
int res=sl+sr;
if(k==1){
cout<<res+sum;
}else{
for(int i=0; i<v.size(); i++){
s.erase(s.upper_bound({d[v[i].ss].ff,v[i].ss}));
s.erase(s.upper_bound({d[v[i].ss].ss,v[i].ss}));
f.insert({d[v[i].ss].ff,v[i].ss});
f.insert({d[v[i].ss].ss,v[i].ss});
int fh=(f.size()-1)/2,sh=s.size()/2;
int pl=(*f.find_by_order(fh)).ff;
int pr=(*s.find_by_order(sh)).ff;
if(f.size()==2) ml=pl;
sl+=abs(d[v[i].ss].ff-ml);
sl+=abs(d[v[i].ss].ss-ml);
sl+=fh*(pl-ml);
sl-=(f.size()-fh)*(pl-ml);
sr-=abs(d[v[i].ss].ff-mr);
sr-=abs(d[v[i].ss].ss-mr);
sr+=sh*(pr-mr);
sr-=(s.size()-sh)*(pr-mr);
ml=pl;
mr=pr;
res=min(res,sl+sr);
}
cout<<res+sum;
}
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:62:19: 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]
62 | for(int i=0; i<v.size(); i++){
| ~^~~~~~~~~
bridge.cpp:68:19: 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]
68 | for(int i=0; i<v.size(); i++){
| ~^~~~~~~~~
bridge.cpp:76:17: 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]
76 | for(int i=0; i<v.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... |