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;
#define int long long
#define ld long double
#define F first
#define S second
#define el '\n'
#define cout(x) for(auto v:x)cout<<v<<' ';cout<<el
#define coutp(x) for(auto v:x)cout<<v.F<<' '<<v.S<<el
#define cin(x) for(auto &v:x)cin>>v;
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
const int N = 2e6 + 5, M = 1e2 + 5, Mod = 1e9 + 7;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};
void solve(int T)
{
int k, n;
cin >> k >> n;
vector<pair<int,int>> v;
int sum = 0;
for (int i = 0; i < n; i++)
{
char zn1, zn2;
int loc1, loc2;
cin >> zn1 >> loc1 >> zn2 >> loc2;
if (zn1 == zn2)
sum += abs(loc1 - loc2);
else
{
if (zn1 == 'B')
swap(loc1, loc2);
v.emplace_back(loc1, loc2);
}
}
sort(all(v));
// coutp(v);
int mn = 1e18;
for (int i = 0; i < v.size(); i++)
{
int cur = 0,cur2=0;
int loc = v[i].F;
for (int j = 0; j < v.size(); j++)
{
cur += abs(v[j].F - v[j].S);
if (min(v[j].F, v[j].S) <= loc && loc <= max(v[j].F, v[j].S))cur += 1;
else cur+=1+2*min(abs(v[j].F-loc),abs(v[j].S-loc));
}
loc = v[i].S;
for (int j = 0; j < v.size(); j++)
{
cur2 += abs(v[j].F - v[j].S);
if (min(v[j].F, v[j].S) <= loc && loc <= max(v[j].F, v[j].S))cur2 += 1;
else cur2+=1+2*min(abs(v[j].F-loc),abs(v[j].S-loc));
}
mn=min({mn,cur,cur2});
// cout<<cur<<' '<<cur2<<el;
}
cout<<mn+sum<<el;
}
int32_t main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; i++)solve(i);
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'void solve(long long int)':
bridge.cpp:42:23: 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]
42 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
bridge.cpp:46:27: 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]
46 | for (int j = 0; j < v.size(); j++)
| ~~^~~~~~~~~~
bridge.cpp:53:27: 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]
53 | for (int j = 0; j < v.size(); j++)
| ~~^~~~~~~~~~
# | 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... |