#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename K> using hset = gp_hash_table<K, null_type>;
template<typename K, typename V> using hmap = gp_hash_table<K, V>;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define smax(x, y) (x = max(x, y))
#define smin(x, y) (x = min(x, y))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i,0,a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i,0,a)
using ll = long long;
using ld = long double;
template<typename T>
using vv = vector<vector<T>>;
using vi = vector<int>;
using ii = array<int, 2>;
using vii = vector<ii>;
using vvi = vv<int>;
using vll = vector<ll>;
using l2 = array<ll, 2>;
using vl2 = vector<l2>;
using vvll = vv<ll>;
template<typename T>
using minq = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using maxq = priority_queue<T>;
const ll M = 1000000007;
const ll infll = M * M;
maxq<int> l;
minq<int> r;
ll ls, rs;
void insert(int x) {
if (!l.empty() && x > l.top()) {
r.push(x);
rs += x;
} else {
l.push(x);
ls += x;
}
int msz = (l.size() + r.size() + 1) / 2;
if (l.size() > msz) {
int z = l.top();
l.pop();
r.push(z);
ls -= z;
rs += z;
} else if (l.size() < msz) {
int z = r.top();
r.pop();
l.push(z);
rs -= z;
ls += z;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k, n;
cin >> k >> n;
ll res{};
vl2 pe;
ll same_side{};
F0R(i, n) {
char p, q;
ll s, t;
cin >> p >> s >> q >> t;
if (p == q) same_side += abs(t - s);
else pe.pb({s, t});
}
n = pe.size();
same_side += n; // distance from people crossing the bridge
sort(all(pe), [&](const l2& a, const l2& b) -> bool {
return a[0] + a[1] < b[0] + b[1];
});
vll pres(n+1); // prefix res
ls = rs = 0;
F0R(i, n) {
insert(pe[i][0]);
insert(pe[i][1]);
pres[i+1] = rs - ls;
}
res += pres[n];
if (k == 2) {
r = {};
l = {};
ls = rs = 0;
R0F(i, n) {
insert(pe[i][0]);
insert(pe[i][1]);
smin(res, pres[i] + rs - ls);
}
}
cout << res + same_side << '\n';
}
Compilation message
bridge.cpp: In function 'void insert(int)':
bridge.cpp:61:18: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::less<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
61 | if (l.size() > msz) {
| ~~~~~~~~~^~~~~
bridge.cpp:67:25: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::less<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
67 | } else if (l.size() < msz) {
| ~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
27 ms |
5084 KB |
Output is correct |
13 |
Correct |
68 ms |
6292 KB |
Output is correct |
14 |
Correct |
46 ms |
4976 KB |
Output is correct |
15 |
Correct |
28 ms |
4024 KB |
Output is correct |
16 |
Correct |
32 ms |
5628 KB |
Output is correct |
17 |
Correct |
42 ms |
6180 KB |
Output is correct |
18 |
Correct |
39 ms |
5804 KB |
Output is correct |
19 |
Correct |
47 ms |
6176 KB |
Output is correct |
20 |
Correct |
37 ms |
6092 KB |
Output is correct |
21 |
Correct |
54 ms |
5968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
0 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
30 ms |
4656 KB |
Output is correct |
26 |
Correct |
47 ms |
5212 KB |
Output is correct |
27 |
Correct |
78 ms |
5888 KB |
Output is correct |
28 |
Correct |
73 ms |
6668 KB |
Output is correct |
29 |
Correct |
63 ms |
6116 KB |
Output is correct |
30 |
Correct |
32 ms |
3496 KB |
Output is correct |
31 |
Correct |
36 ms |
5864 KB |
Output is correct |
32 |
Correct |
51 ms |
6204 KB |
Output is correct |
33 |
Correct |
50 ms |
6332 KB |
Output is correct |
34 |
Correct |
58 ms |
6180 KB |
Output is correct |
35 |
Correct |
42 ms |
6076 KB |
Output is correct |
36 |
Correct |
56 ms |
5960 KB |
Output is correct |
37 |
Correct |
23 ms |
4676 KB |
Output is correct |