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 pb push_back
#define F first
#define S second
#define FOR(i,a, b) for (int i=a; i<=(b); i++)
#define F0R(i,a, b) for (int i=a; i>=(b); i--)
using namespace std;
int n, k, slo, sup, kep, pref[200005];
long long ans;
vector<pair<int,int>> v;
priority_queue<int>lo;
priority_queue<int,vector<int>,greater<int>>hi;
void ins (int x){
if (lo.empty()){lo.push(x);slo+=x;return;}
if (x > lo.top())
hi.push(x), sup+=x;
else
lo.push(x), slo+=x;
if (hi.size() > lo.size()){
int top = hi.top();
hi.pop();
lo.push(top);
sup -= top;
slo += top;
}
else if (lo.size() > hi.size()+1){
int top = hi.top();
lo.pop();
hi.push(top);
slo -= top;
sup += top;
}
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> k >> n;
FOR(i,1,n){
char c1, c2;
int x, y;
cin >> c1 >> x >> c2 >> y;
if (c1 == c2)
kep += abs(x-y);
else
v.pb({x,y});
}
n = v.size();
FOR(i,0,n-1){
ins(v[i].F);
ins(v[i].S);
pref[i] = sup-slo;
}
ans = pref[n-1];
if (k == 2){
while (hi.size())hi.pop(); sup=0;
while (lo.size())lo.pop(); slo=0;
F0R(i,n-1, 1){
ins(v[i].F);
ins(v[i].S);
ans = min(ans, (long long)sup-slo+pref[i-1]);
}
}
cout << ans + kep + n;
}
Compilation message (stderr)
bridge.cpp: In function 'int32_t main()':
bridge.cpp:72:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
72 | while (hi.size())hi.pop(); sup=0;
| ^~~~~
bridge.cpp:72:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
72 | while (hi.size())hi.pop(); sup=0;
| ^~~
bridge.cpp:73:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
73 | while (lo.size())lo.pop(); slo=0;
| ^~~~~
bridge.cpp:73:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
73 | while (lo.size())lo.pop(); slo=0;
| ^~~
# | 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... |