This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
bool comp(pair<int,int>a,pair<int,int>b)
{
return (b.second>a.second);
}
void solve()
{
int k,n;
cin>>k>>n;
long long ans=0;
vector<pair<int,int>>d;
int z=0;
for (int i=0;i<n;i++)
{
char a,b;
int x,y;
cin>>a>>x>>b>>y;
if (a==b)
ans+=abs(x-y);
else
{
z+=abs(x-y);
if (a=='B')
swap(x,y);
d.push_back({x,y});
}
}
if (d.size())
{
long long f=1e15+10;
for (auto i:d)
{
int z=i.first;
long long g=0;
for (auto j:d)
g+=(abs(j.first-z)+abs(j.second-z))+1;
f=min(f,g);
g=0;
z=i.second;
for (auto j:d)
g+=(abs(j.first-z)+abs(j.second-z))+1;
f=min(f,g);
}
ans+=f;
}
cout<<ans<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# | 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... |