#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define onlycin ios_base::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
/*typedef tree<
pair < int, int >,
null_type,
less< pair < int, int > >,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
// find_by_order() order_of_key()
const int MAXN = (int)5e5+228;
const char nxtl = '\n';
const int mod = (int)1e9+7;
const double eps = (double)1e-7;
template<typename T> inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T> inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
int n, k;
bool side[2][MAXN];
int house[2][MAXN];
inline ll get(int pos) {
ll ret = 0;
rep(i, 1, n) {
if(side[0][i] == side[1][i]) continue;
ret += abs(pos - house[0][i])+abs(pos - house[1][i]) + 1;
}
return ret;
}
int main() {
#ifdef accepted
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
onlycin;
cin >> k >> n;
ll cur = 0, res = LLONG_MAX;
rep(i, 1, n) {
char x, y; int q, w;
cin >> x; cin >> q; cin >> y; cin >> w;
if(x == 'B') side[0][i] = 1;
if(y == 'B') side[1][i] = 1;
house[0][i] = q; house[1][i] = w;
if(side[0][i] == side[1][i]) cur += abs(house[0][i]-house[1][i]);
}
int l = 0, r = n+1;
while(r - l > 5) {
int mid = l + r >> 1;
if(get(mid) > get(mid+1)) l = mid;
else r = mid;
}
updmax(l, 1); updmin(r, n);
rep(i, l, r) if(updmin(res, get(i)));
cout << res+cur << nxtl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:65:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
480 KB |
Output is correct |
3 |
Correct |
2 ms |
480 KB |
Output is correct |
4 |
Incorrect |
2 ms |
496 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
496 KB |
Output is correct |
2 |
Correct |
1 ms |
496 KB |
Output is correct |
3 |
Correct |
2 ms |
496 KB |
Output is correct |
4 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
584 KB |
Output is correct |
2 |
Correct |
1 ms |
584 KB |
Output is correct |
3 |
Incorrect |
1 ms |
584 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Incorrect |
2 ms |
600 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Incorrect |
2 ms |
620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |