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;
typedef long long ll;
const int N = 1e5 + 5;
int k, n;
ll same_side = 0, l = 0, r = 0, p[N], s[N], ans = 1e18;
vector<pair<int,int>> v;
priority_queue<int> lf;
priority_queue<int, vector<int>, greater<int>> rg;
void push(int x)
{
if(lf.empty() || x <= lf.top()) {
lf.push(x);
l += x;
} else {
rg.push(x);
r += x;
}
if((int)lf.size() - 1 > rg.size()) {
int t = lf.top();
l -= t;
r += t;
lf.pop();
rg.push(t);
} else if(lf.size() < rg.size()) {
int t = rg.top();
r -= t;
l += t;
rg.pop();
lf.push(t);
}
}
int main()
{
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> k >> n;
for(int i = 0; i < n; i++) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a == b) {
same_side += abs(x - y);
} else {
v.push_back({x, y});
}
}
sort(v.begin(), v.end(), [&](auto x, auto y){
return x.first + x.second < y.first + y.second;
});
for(int i = 0; i < v.size(); i++) {
push(v[i].first);
push(v[i].second);
p[i] = r - l;
}
if(k == 1) {
cout << same_side + p[v.size() - 1] + v.size() << '\n';
return 0;
}
while(!lf.empty()) lf.pop();
while(!rg.empty()) rg.pop();
l = 0, r = 0;
for(int i = v.size() - 1; i > 0; i--) {
push(v[i].first);
push(v[i].second);
ans = min(ans, r - l + p[i - 1]);
}
cout << same_side + ans + v.size() << '\n';
}
Compilation message (stderr)
bridge.cpp: In function 'void push(int)':
bridge.cpp:22:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if((int)lf.size() - 1 > rg.size()) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:57:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < v.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... |