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<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; i<n; i++){
int s,t;
char p,q;
cin>>p>>s>>q>>t;
if(p==q){
sum+=abs(s-t);
}else{
sum++;
v.push_back({s,i});
v.push_back({t,i});
d.push_back({s,t});
}
}
int sl=0,sr=0;
ordered_set f,s;
vector<bool> vis(n);
sort(v.begin(),v.end());
int ml=0,mr=v[v.size()/2].ff;
for(int i=0; i<v.size(); i++){
sr+=abs(mr-v[i].ff);
s.insert({v[i].ff,v[i].ss});
}
int res=sl+sr;
if(k==1){
cout<<res+sum;
}else{
for(int i=0; i<v.size(); i++){
if(!vis[v[i].ss]){
vis[v[i].ss]=1;
s.erase({d[v[i].ss].ff,v[i].ss});
s.erase({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()/2,sh=s.size()/2;
int pl=(*f.find_by_order(fh)).ff;
int pr=(*s.find_by_order(sh)).ff;
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:60: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]
60 | for(int i=0; i<v.size(); i++){
| ~^~~~~~~~~
bridge.cpp:68: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]
68 | 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... |