This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 max(s[a], t[a]) < max(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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |