#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5;
int k, n, s[N + 5], t[N + 5];
char p[N + 5], q[N + 5];
namespace sub2 {
int main(int n, int k, int s[], int t[], char p[], char q[]) {
vector<int> arr;
int init = 0;
for (int i = 1; i <= n; i++) {
if (p[i] == q[i]) init += abs(s[i] - t[i]);
else arr.push_back(s[i]), arr.push_back(t[i]), init++;
}
sort(arr.begin(), arr.end());
int ans = 0, mid = arr.size()/2;
for (auto&v : arr) ans += abs(v - arr[mid]);
return init + ans;
}
}
namespace sub5 {
void balance(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum) {
if (f.size() < se.size()) f.insert(*se.begin()), fSum += *se.begin(), seSum -= *se.begin(), se.erase(se.find(*se.begin()));
else if (f.size() > se.size() + 1) se.insert(*f.rbegin()), seSum += *f.rbegin(), fSum -= *f.rbegin(), f.erase(f.find(*f.rbegin()));
}
int compute(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum) {
if(f.empty()) return 1e9;
int median = *f.rbegin(), fSz = f.size(), seSz = se.size();
return median*fSz - fSum + seSum - seSz*median;
}
void add(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum, int v) {
if (f.empty() || v <= *f.rbegin()) f.insert(v), fSum += v;
else se.insert(v), seSum += v;
balance(f, se, fSum, seSum);
}
void remove(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum, int v) {
if (f.find(v) != f.end()) f.erase(f.find(v)), fSum -= v;
else se.erase(se.find(v)), seSum -= v;
balance(f, se, fSum, seSum);
}
int main(int n, int k, int s[], int t[], char p[], char q[]) {
vector<int> arr;
int init = 0;
for (int i = 1; i <= n; i++){
if (p[i] == q[i]) init += abs(s[i] - t[i]);
else arr.push_back(i), init++;
}
sort(arr.begin(), arr.end(), [&](int a, int b) { return s[a] + t[a] < s[b] + t[b]; });
multiset<int> f, se, thi, four;
int fSum = 0, seSum = 0, thiSum = 0, fourSum = 0;
for (int i = 0; i < arr.size(); i++) {
add(thi, four, thiSum, fourSum, s[arr[i]]);
add(thi, four, thiSum, fourSum, t[arr[i]]);
}
int ans = compute(thi, four, thiSum, fourSum);
for (int i = 0; i < arr.size(); i++) {
remove(thi, four, thiSum, fourSum, s[arr[i]]);
remove(thi, four, thiSum, fourSum, t[arr[i]]);
add(f, se, fSum, seSum, s[arr[i]]);
add(f, se, fSum, seSum, t[arr[i]]);
ans = min(ans, compute(f, se, fSum, seSum) + compute(thi, four, thiSum, fourSum));
}
return init + ans;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> k >> n;
for (int i = 1; i <= n; i++) cin >> p[i] >> s[i] >> q[i] >> t[i];
int ans = sub2::main(n, k, s, t, p, q);
if (k == 2) ans = min(ans, sub5::main(n, k, s, t, p, q));
cout << ans << '\n';
return 0;
}
Compilation message
bridge.cpp: In function 'long long int sub5::main(long long int, long long int, long long int*, long long int*, char*, char*)':
bridge.cpp:62:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
bridge.cpp:67:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
344 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 |
15 ms |
4944 KB |
Output is correct |
13 |
Correct |
33 ms |
5388 KB |
Output is correct |
14 |
Correct |
24 ms |
5200 KB |
Output is correct |
15 |
Correct |
19 ms |
3652 KB |
Output is correct |
16 |
Correct |
19 ms |
5336 KB |
Output is correct |
17 |
Correct |
23 ms |
5412 KB |
Output is correct |
18 |
Correct |
28 ms |
5320 KB |
Output is correct |
19 |
Correct |
32 ms |
5372 KB |
Output is correct |
20 |
Correct |
21 ms |
5320 KB |
Output is correct |
21 |
Correct |
28 ms |
5396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
0 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 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 |
480 KB |
Output is correct |
20 |
Correct |
1 ms |
372 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
480 KB |
Output is correct |
15 |
Correct |
2 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
143 ms |
12960 KB |
Output is correct |
26 |
Correct |
234 ms |
13208 KB |
Output is correct |
27 |
Correct |
292 ms |
13972 KB |
Output is correct |
28 |
Correct |
285 ms |
14516 KB |
Output is correct |
29 |
Correct |
276 ms |
14480 KB |
Output is correct |
30 |
Correct |
127 ms |
8044 KB |
Output is correct |
31 |
Correct |
128 ms |
13972 KB |
Output is correct |
32 |
Correct |
149 ms |
14448 KB |
Output is correct |
33 |
Correct |
118 ms |
14164 KB |
Output is correct |
34 |
Correct |
173 ms |
14584 KB |
Output is correct |
35 |
Correct |
155 ms |
14032 KB |
Output is correct |
36 |
Correct |
166 ms |
14228 KB |
Output is correct |
37 |
Correct |
141 ms |
12976 KB |
Output is correct |