#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 - loc) + abs(v[j].S - loc) + 1;
}
loc = v[i].S;
for (int j = 0; j < v.size(); j++)
{
cur2 += abs(v[j].F - loc) + abs(v[j].S - loc) + 1;
}
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
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:51: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]
51 | for (int j = 0; j < v.size(); j++)
| ~~^~~~~~~~~~
bridge.cpp: In function 'int32_t main()':
bridge.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |