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 int long long
#define pb push_back
#define F first
#define S second
#define B begin()
#define E end()
using namespace std;
vector<pair<int,int>>v;
int f(int x)
{
int g=0;
for(int i=0;i<v.size();i++)
{
g+=abs(v[i].F-x )+abs(v[i].S-x);
}
return g;
}
signed main()
{
int n,k;
cin>>k>>n;
int ans=0;
for(int i=0;i<n;i++)
{
char x,y;
int a,b;
cin>>x>>a>>y>>b;
if(x==y)
{
ans+=abs(a-b);
}
else v.pb({a,b}),ans++;
}
sort(v.B,v.E);
int x=v.size()/2;
int l=-3,r=1000000005;
int y=200;
int mn=1e18;
while(y--)
{
int mid1=l+(r-l)/3;
int mid2=l+((r-l)/3)*2;
mn=min(mn,f(mid1));
mn=min(mn,f(mid2));
if(f(mid2)<=f(mid1))
{
l=mid1;
}
else
r=mid2;
}
mn=min(mn,f(l));
cout<<min(mn,f(r))+ans<<endl;
}
Compilation message (stderr)
bridge.cpp: In function 'long long int f(long long int)':
bridge.cpp:13:18: 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]
13 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:37:9: warning: unused variable 'x' [-Wunused-variable]
37 | int x=v.size()/2;
| ^
# | 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... |