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>
using namespace std;
int k, n, b, d;
char a, c;
vector<int> v;
vector<pair<int, int>> p;
long long res, mn=LLONG_MAX;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>k>>n;
if (k==1)
{
for (int i=0; i<n; i++)
{
cin>>a>>b>>c>>d;
if (a==c)
{
res+=abs(b-d);
continue;
}
v.push_back(b);
v.push_back(d);
res++;
}
sort(v.begin(), v.end());
int md=v[v.size()/2];
for (auto x:v) res+=abs(x-md);
cout<<res;
}
else
{
for (int i=0; i<n; i++)
{
cin>>a>>b>>c>>d;
if (a==c)
{
res+=abs(b-d);
continue;
}
p.push_back({min(b, d), max(b, d)});
res++;
}
sort(p.begin(), p.end());
for (int i=0; i<(int)p.size()-1; i++)
{
long long tmp=0;
int ed=i, st=i+1;
vector<int> t1, t2;
for (int j=0; j<=ed; j++) t1.push_back(p[j].first), t1.push_back(p[j].second);
for (int j=st; j<p.size(); j++) t2.push_back(p[j].first), t2.push_back(p[j].second);
int md1=t1[(int)t1.size()/2], md2=t2[(int)t2.size()/2];
for (auto x:t1) tmp+=abs(x-md1);
for (auto x:t2) tmp+=abs(x-md2);
mn=min(mn, tmp);
}
cout<<res+mn;
}
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:54:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int j=st; j<p.size(); j++) t2.push_back(p[j].first), t2.push_back(p[j].second);
| ~^~~~~~~~~| # | 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... |