#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
main() {
int K, N; scanf("%lld %lld", &K, &N);
int ans = 0;
vector<pair<int, int> > lst;
for (int i = 0; i < N; i++) {
char c1, c2;
int i1, i2;
scanf(" %c %lld %c %lld", &c1, &i1, &c2, &i2);
if (c1 == c2) ans += abs(i2 - i1);
else lst.emplace_back(i1, i2);
}
sort(lst.begin(), lst.end(), [](ii i, ii j) {
return i.first + i.second < j.first + j.second;
});
N = lst.size();
int pre[N + 5], suf[N + 5];
memset(pre, 0, sizeof(pre));
memset(suf, 0, sizeof(suf));
int left = 0, right = 0;
priority_queue<int> l;
priority_queue<int, vector<int>, greater<int> > r;
for (int i = 0; i < N; i++) {
r.push(lst[i].first); r.push(lst[i].second);
right += lst[i].first + lst[i].second;
while (l.size() < r.size() || r.top() < l.top()) {
right -= r.top();
left += r.top();
l.push(r.top()); r.pop();
}
while (l.size() > r.size()) {
right += l.top();
left -= l.top();
r.push(l.top()); l.pop();
}
pre[i] = right - r.size() * l.top() + l.top() * l.size() - left;
}
left = right = 0;
while (!r.empty()) r.pop();
while (!l.empty()) l.pop();
for (int i = N - 1; i >= 0; --i) {
r.push(lst[i].first); r.push(lst[i].second);
right += lst[i].first + lst[i].second;
while (l.size() < r.size() || r.top() < l.top()) {
right -= r.top();
left += r.top();
l.push(r.top()); r.pop();
}
while (l.size() > r.size()) {
right += l.top();
left -= l.top();
r.push(l.top()); l.pop();
}
suf[i] = right - r.size() * l.top() + l.top() * l.size() - left;
}
int mini = min(suf[0], pre[N - 1]);
// for (int i = 0; i < N; i++)
// printf("%lld %lld\n", pre[i], suf[i]);
if (K == 1) {
printf("%lld\n", mini + N + ans);
return 0;
}
for (int i = 0; i < N - 1; i++)
mini = min(mini, suf[i + 1] + pre[i]);
printf("%lld\n", mini + N + ans);
}
Compilation message
bridge.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
7 | main() {
| ^~~~
bridge.cpp: In function 'int main()':
bridge.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | int K, N; scanf("%lld %lld", &K, &N);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
bridge.cpp:15:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf(" %c %lld %c %lld", &c1, &i1, &c2, &i2);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
276 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
308 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 |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
48 ms |
7092 KB |
Output is correct |
13 |
Correct |
100 ms |
8672 KB |
Output is correct |
14 |
Correct |
88 ms |
7048 KB |
Output is correct |
15 |
Correct |
61 ms |
4756 KB |
Output is correct |
16 |
Correct |
55 ms |
8012 KB |
Output is correct |
17 |
Correct |
86 ms |
8668 KB |
Output is correct |
18 |
Correct |
90 ms |
8212 KB |
Output is correct |
19 |
Correct |
95 ms |
8624 KB |
Output is correct |
20 |
Correct |
61 ms |
8216 KB |
Output is correct |
21 |
Correct |
98 ms |
8400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 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 |
0 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 |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
236 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
268 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
296 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 |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
296 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 |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
292 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
280 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
312 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
272 KB |
Output is correct |
7 |
Correct |
1 ms |
272 KB |
Output is correct |
8 |
Correct |
0 ms |
280 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 |
288 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
328 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 |
25 |
Correct |
53 ms |
7088 KB |
Output is correct |
26 |
Correct |
80 ms |
7200 KB |
Output is correct |
27 |
Correct |
101 ms |
8112 KB |
Output is correct |
28 |
Correct |
106 ms |
8680 KB |
Output is correct |
29 |
Correct |
105 ms |
8640 KB |
Output is correct |
30 |
Correct |
52 ms |
4408 KB |
Output is correct |
31 |
Correct |
53 ms |
7888 KB |
Output is correct |
32 |
Correct |
86 ms |
8600 KB |
Output is correct |
33 |
Correct |
80 ms |
8272 KB |
Output is correct |
34 |
Correct |
108 ms |
8672 KB |
Output is correct |
35 |
Correct |
62 ms |
8144 KB |
Output is correct |
36 |
Correct |
108 ms |
8364 KB |
Output is correct |
37 |
Correct |
42 ms |
7044 KB |
Output is correct |