이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int64_t L, R;
priority_queue<int, vector<int>, greater<int>> rpq;
priority_queue<int> lpq;
const int INF = 1e9 + 1;
void insert(int x){
int med = (lpq.size() ? lpq.top() : INF);
if(x <= med){
lpq.push(x);
L += int64_t(x);
}
else{
rpq.push(x);
R += int64_t(x);
}
if(rpq.size() + 1 < lpq.size()){
assert(!lpq.empty());
int nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
L -= int64_t(nxt);
R += int64_t(nxt);
}
else if(lpq.size() < rpq.size()){
assert(!rpq.empty());
int nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
R -= int64_t(nxt);
L += int64_t(nxt);
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> k >> n;
vector<array<int, 2>> v;
int64_t same = 0;
for(int i = 0; i < n; i++){
char a, b;
int p, q;
cin >> a >> p >> b >> q;
if(a == b) same += abs(p - q);
else v.push_back({p, q});
}
sort(v.begin(), v.end(), [](array<int, 2>& a, array<int, 2>& b){
return (a[0] + a[1]) < (b[0] + b[1]);
});
n = v.size();
same += n;
L = R = 0;
vector<int64_t> pref(n);
for(int i = 0; i < n; i++){
insert(v[i][0]);
insert(v[i][1]);
pref[i] = R - L;
}
int64_t ans = pref[n-1];
if(k == 1){
cout << same + ans << '\n';
return 0;
}
while(!lpq.empty()) lpq.pop();
while(!rpq.empty()) rpq.pop();
L = R = 0;
for(int i = n-1; i > 0; --i){
insert(v[i][0]);
insert(v[i][1]);
ans = min(ans, pref[i-1] + R - L);
}
cout << same + ans << '\n';
return 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... |