#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5;
int n, k;
vector<pair<int, int>> v;
int l[N], r[N];
template<class T>
void solve(T it, T end, int *result) {
multiset<int> ls, mr;
int sls = 0, smr = 0, c = 0;
while (it != end) {
ls.insert(it->first);
sls += it->first;
mr.insert(it->second);
smr += it->second;
if (*ls.rbegin() > *mr.begin()) {
sls += *mr.begin() - *ls.rbegin();
smr += *ls.rbegin() - *mr.begin();
ls.insert(*mr.begin());
mr.insert(*ls.rbegin());
ls.erase(ls.find(*ls.rbegin()));
mr.erase(mr.find(*mr.begin()));
}
result[c++] = smr - sls;
it++;
}
}
int solveTestCase() {
cin >> k >> n;
int temp = 0;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q)
temp += abs(t - s);
else
v.emplace_back(s, t);
}
n = v.size();
sort(v.begin(), v.end(), [](auto &p1, auto &p2) {
return p1.first + p1.second < p2.first + p2.second;
});
solve(v.begin(), v.end(), l);
solve(v.rbegin(), v.rend(), r);
int ans = temp + n + min(l[n - 1], r[n - 1]);
if (k == 1)
return cout << ans, 0;
for (int i = 0; i < n - 1; i++)
ans = min(ans, temp + n + l[i] + r[n - 2 - i]);
cout << ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--)
solveTestCase();
}
Compilation message
bridge.cpp: In function 'long long int solveTestCase()':
bridge.cpp:64:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
152 ms |
13676 KB |
Output is correct |
13 |
Correct |
311 ms |
15208 KB |
Output is correct |
14 |
Correct |
267 ms |
12844 KB |
Output is correct |
15 |
Correct |
145 ms |
9068 KB |
Output is correct |
16 |
Correct |
143 ms |
14564 KB |
Output is correct |
17 |
Correct |
211 ms |
15336 KB |
Output is correct |
18 |
Correct |
137 ms |
14964 KB |
Output is correct |
19 |
Correct |
210 ms |
15208 KB |
Output is correct |
20 |
Correct |
184 ms |
14824 KB |
Output is correct |
21 |
Correct |
217 ms |
14952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
432 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
154 ms |
13764 KB |
Output is correct |
26 |
Correct |
248 ms |
13800 KB |
Output is correct |
27 |
Correct |
324 ms |
14568 KB |
Output is correct |
28 |
Correct |
329 ms |
15284 KB |
Output is correct |
29 |
Correct |
326 ms |
15192 KB |
Output is correct |
30 |
Correct |
132 ms |
8304 KB |
Output is correct |
31 |
Correct |
135 ms |
14568 KB |
Output is correct |
32 |
Correct |
224 ms |
15208 KB |
Output is correct |
33 |
Correct |
131 ms |
14824 KB |
Output is correct |
34 |
Correct |
208 ms |
15284 KB |
Output is correct |
35 |
Correct |
179 ms |
14696 KB |
Output is correct |
36 |
Correct |
211 ms |
14936 KB |
Output is correct |
37 |
Correct |
104 ms |
13672 KB |
Output is correct |