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>
#define int long long
using namespace std;
const int INF = 1e18;
const int mxN = 1e5 + 10;
int n, k;
char p[mxN], q[mxN];
int s[mxN], t[mxN];
namespace full {
multiset<int> L, R;
int sumL = 0, sumR = 0;
int f1[mxN], f2[mxN];
int calc() {
int tmp = INF;
if(L.size() > 0) {
int l = *(-- L.end());
tmp = min(tmp, l * (long long)L.size() - sumL + sumR - l * (long long)R.size());
}
if(R.size() > 0) {
int r = *R.begin();
tmp = min(tmp, r * (long long)L.size() - sumL + sumR - r * (long long)R.size());
}
return tmp;
}
void add(int x) {
if(L.size() > 0 && *(-- L.end()) > x) {
sumL += x;
L.insert(x);
} else {
sumR += x;
R.insert(x);
}
while(L.size() > R.size() + 1) {
sumR += *(-- L.end());
R.insert(*(-- L.end()));
sumL -= *(-- L.end());
L.erase(-- L.end());
}
while(R.size() > L.size()) {
sumL += *R.begin();
L.insert(*R.begin());
sumR -= *R.begin();
R.erase(R.begin());
}
}
void solve() {
int sum = 0;
vector<int> ar;
for(int i = 1; i <= n; i ++) {
if(p[i] == q[i]) sum += abs(s[i] - t[i]);
else {
ar.push_back(i);
}
}
sort(ar.begin(), ar.end(), [&](const int &x, const int &y) {
return s[x] + t[x] < s[y] + t[y];
});
for(int i = 0; i < ar.size(); i ++) {
int id = ar[i];
add(s[id]);
add(t[id]);
f1[i] = calc();
}
L.clear(); R.clear();
sumL = 0; sumR = 0;
for(int i = ar.size() - 1; i >= 0; i --) {
int id = ar[i];
add(s[id]);
add(t[id]);
f2[i] = calc();
}
int result = INF;
if(k >= 1) result = min({result, f1[ar.size() - 1], f2[0]});
if(k >= 2) {
for(int i = 0; i + 1 < ar.size(); i ++) {
result = min(result, f1[i] + f2[i + 1]);
}
}
cout << sum + result + ar.size() << "\n";
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> k >> n;
for(int i = 1; i <= n; i ++) cin >> p[i] >> s[i] >> q[i] >> t[i];
full::solve();
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'void full::solve()':
bridge.cpp:76:26: 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]
76 | for(int i = 0; i < ar.size(); i ++) {
| ~~^~~~~~~~~~~
bridge.cpp:98:34: 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]
98 | for(int i = 0; i + 1 < ar.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... |