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;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
struct median {
multiset<int> Sl;
multiset<int> Sr;
long long left_sum = 0;
long long right_sum = 0;
void clear() {
Sl.clear();
Sr.clear();
left_sum = 0;
right_sum = 0;
}
void insert(int x, int y) {
if (Sr.empty() || x <= *Sr.begin()) {
Sl.insert(x);
left_sum += x;
}
else {
Sr.insert(x);
right_sum += x;
}
if (Sr.empty() || y <= *Sr.begin()) {
Sl.insert(y);
left_sum += y;
}
else {
Sr.insert(y);
right_sum += y;
}
if (Sl.size() > Sr.size()) {
left_sum -= *Sl.rbegin();
right_sum += *Sl.rbegin();
Sr.insert(*Sl.rbegin());
Sl.erase(prev(Sl.end()));
}
else if (Sl.size() < Sr.size()) {
right_sum -= *Sr.begin();
left_sum += *Sr.begin();
Sl.insert(*Sr.begin());
Sr.erase(Sr.begin());
}
}
long long get() {
return right_sum - left_sum;
};
} med;
void just_do_it() {
int K, N;
cin >> K >> N;
vector<int> A;
long long add = 0;
for (int i = 0; i < N; i++) {
char P, Q;
int S, T;
cin >> P >> S >> Q >> T;
if (P == Q) {
add += abs(S - T);
}
else {
add++;
if (K == 1) {
med.insert(S, T);
}
}
}
if (K == 1) {
cout << med.get() + add;
return;
}
}
# | 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... |