#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ii = pair<ll, ll>;
#define fastIO ios::sync_with_stdio(0); cin.tie(0);
#define fi first
#define se second
#define pb push_back
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define MASK(x) 1ll << (x)
template<class X, class Y>
bool minimize(X &x, const Y &y) {
X eps = 1e-9;
if (x > y + eps) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
X eps = 1e-9;
if (x + eps < y) {
x = y;
return true;
} else return false;
}
const int N = 1e6 + 7, oo = 1e9 + 7;
int n, k;
vector<ii> b;
ll lsum, rsum, pre[N];
bool cmp(ii x, ii y) {
return (x.fi + x.se < y.fi + y.se);
}
priority_queue<int> mx;
priority_queue<int, vector<int>, greater<int>> mn;
void add(int x) {
int med = (mn.empty() ? oo : mx.top());
if (x <= med) lsum += x, mx.push(x);
else mn.push(x), rsum += x;
while (sz(mn) >= sz(mx)) {
int nxt = mn.top(); mn.pop();
rsum -= nxt; lsum += nxt; mx.push(nxt);
}
while (sz(mx) - 1 > sz(mn)) {
int nxt = mx.top(); mx.pop();
lsum -= nxt; rsum += nxt; mn.push(nxt);
}
}
signed main() {
fastIO;
if (fopen("bridge.inp", "r")) {
freopen("bridge.inp", "r", stdin);
freopen("bridge.out", "w", stdout);
}
cin >> k >> n; ll ans = 0, same = 0;
for (int i = 1, x1, y1; i <= n; i++) {
char x, y; cin >> x >> x1 >> y >> y1;
if (x == y) same += abs(y1 - x1); else b.pb({x1, y1});
}
sort(all(b), cmp);
for (int i = 0; i < sz(b); i++) {
add(b[i].fi); add(b[i].se);
ans = pre[i] = rsum - lsum;
}
if (k == 2) {
lsum = rsum = 0;
while (!mn.empty()) mn.pop();
while (!mx.empty()) mx.pop();
for (int i = sz(b) - 1; i > 0; i--) {
add(b[i].fi); add(b[i].se);
minimize(ans, rsum - lsum + pre[i - 1]);
}
}
cout << ans + same + sz(b);
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen("bridge.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen("bridge.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
31 ms |
4648 KB |
Output is correct |
13 |
Correct |
69 ms |
6308 KB |
Output is correct |
14 |
Correct |
64 ms |
4868 KB |
Output is correct |
15 |
Correct |
43 ms |
3772 KB |
Output is correct |
16 |
Correct |
39 ms |
5568 KB |
Output is correct |
17 |
Correct |
57 ms |
6244 KB |
Output is correct |
18 |
Correct |
46 ms |
5816 KB |
Output is correct |
19 |
Correct |
61 ms |
6136 KB |
Output is correct |
20 |
Correct |
42 ms |
5848 KB |
Output is correct |
21 |
Correct |
61 ms |
5916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
360 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
50 ms |
4600 KB |
Output is correct |
26 |
Correct |
77 ms |
4824 KB |
Output is correct |
27 |
Correct |
102 ms |
5552 KB |
Output is correct |
28 |
Correct |
101 ms |
6172 KB |
Output is correct |
29 |
Correct |
98 ms |
6208 KB |
Output is correct |
30 |
Correct |
52 ms |
3528 KB |
Output is correct |
31 |
Correct |
53 ms |
5552 KB |
Output is correct |
32 |
Correct |
81 ms |
6160 KB |
Output is correct |
33 |
Correct |
79 ms |
5824 KB |
Output is correct |
34 |
Correct |
86 ms |
6208 KB |
Output is correct |
35 |
Correct |
49 ms |
5728 KB |
Output is correct |
36 |
Correct |
84 ms |
5872 KB |
Output is correct |
37 |
Correct |
30 ms |
4632 KB |
Output is correct |