#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, a, b) for (int i = (a); i >= (b); i--)
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int base = 31;
const ll mod = 1e9 + 7;
const ll oo = 1e16;
const int N = 1e6 + 5;
const int M = 1e6;
vector<pii> v;
bool cmp(pii a, pii b){
return (a.fi + a.se) < (b.fi + b.se);
}
struct node{
ll cnt, sum;
};
struct segtree{
node st[4 * N];
node combine(node a, node b){
return {a.cnt + b.cnt, a.sum + b.sum};
}
void update(int id, int l, int r, int i, int v){
if (i < l || r < i) return;
if (l == r){
st[id].cnt += v;
st[id].sum = l * st[id].cnt;
return;
}
int mid = (l + r) >> 1;
update(2 * id, l, mid, i, v), update(2 * id + 1, mid + 1, r, i, v);
st[id] = combine(st[2 * id], st[2 * id + 1]);
}
node get(int id, int l, int r, int u, int v){
if (v < l || r < u) return {0, 0};
if (u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return combine(get(2 * id, l, mid, u, v), get(2 * id + 1, mid + 1, r, u, v));
}
int find_kth(int id, int l, int r, int k){
if (l == r) return l;
int mid = (l + r) >> 1;
if (st[2 * id].cnt >= k) return find_kth(2 * id, l, mid, k);
return find_kth(2 * id + 1, mid + 1, r, k - st[2 * id].cnt);
}
ll find_cost(int sz){
ll med = find_kth(1, 0, M, (1 + sz) >> 1);
node lower_ans = get(1, 0, M, 0, med);
node higher_ans = get(1, 0, M, med + 1, M);
return med * lower_ans.cnt - lower_ans.sum + higher_ans.sum - med * higher_ans.cnt;
}
} Left, Right, All;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int K, n;
cin >> K >> n;
ll res = 0;
forr(i, 1, n) {
char b1, b2;
int x1, x2;
cin >> b1 >> x1 >> b2 >> x2;
if (b1 == b2) res += abs(x1 - x2);
else
v.pb({x1, x2});
}
res += v.size();
sort(all(v), cmp);
// K = 1
forf(i, 0, v.size()){
All.update(1, 0, M, v[i].fi, 1);
All.update(1, 0, M, v[i].se, 1);
}
ll cost1 = All.find_cost(2 * v.size());
ll cost2 = oo;
if (K == 2 && v.size() >= 2){
forf(i, 0, v.size()) {
Right.update(1, 0, M, v[i].fi, 1);
Right.update(1, 0, M, v[i].se, 1);
}
int cntLeft = 0;
forr(i, 0, v.size() - 2){
Left.update(1, 0, M, v[i].fi, 1);
Left.update(1, 0, M, v[i].se, 1);
Right.update(1, 0, M, v[i].fi, -1);
Right.update(1, 0, M, v[i].se, -1);
cntLeft++;
cost2 = min(cost2, Left.find_cost(2 * cntLeft) + Right.find_cost(2 * (v.size() - cntLeft)));
}
}
cout << res + min(cost1, cost2) << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:4:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define forf(i, a, b) for (int i = (a); i < (b); i++)
| ^
bridge.cpp:106:5: note: in expansion of macro 'forf'
106 | forf(i, 0, v.size()){
| ^~~~
bridge.cpp:4:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define forf(i, a, b) for (int i = (a); i < (b); i++)
| ^
bridge.cpp:116:7: note: in expansion of macro 'forf'
116 | forf(i, 0, v.size()) {
| ^~~~
bridge.cpp:2:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
2 | #define forr(i, a, b) for (int i = (a); i <= (b); i++)
| ^
bridge.cpp:122:7: note: in expansion of macro 'forr'
122 | forr(i, 0, v.size() - 2){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
5 ms |
20316 KB |
Output is correct |
5 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Incorrect |
1 ms |
8792 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |