이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
const int mxN=1e5;
int k, n, med;
ll ba, a1[mxN+1], a2=LLONG_MAX, bs, ts;
vector<pii> ps;
priority_queue<int> bpq;
priority_queue<int, vector<int>, greater<int>> tpq;
inline void upd(int x) {
if(x<med)
bpq.push(x), bs+=x;
else
tpq.push(x), ts+=x;
if(bpq.size()+2<=tpq.size()) {
bpq.push(med), bs+=med;
med=tpq.top(), tpq.pop(), ts-=med;
} else if(bpq.size()>=tpq.size()+2) {
tpq.push(med), ts+=med;
med=bpq.top(), bpq.pop(), bs-=med;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
for(int i=0; i<n; ++i) {
char z1, z2;
pii pi;
cin >> z1 >> pi.fi >> z2 >> pi.se;
if(z1!=z2) {
ps.push_back(pi);
++ba;
} else
ba+=abs(pi.fi-pi.se);
}
sort(ps.begin(), ps.end(), [](const pii &a, const pii &b) {
return a.fi+a.se<b.fi+b.se;
});
// cout << "hi1" << endl;
for(int i=0; i<ps.size(); ++i) {
upd(ps[i].fi), upd(ps[i].se);
a1[i+1]=ts-(ll)tpq.size()*med+((ll)bpq.size()-1)*med-bs;
}
// cout << "hi2" << endl;
if(k==1) {
// cout << ba+a1[3] << endl;
cout << ba+a1[ps.size()];
return 0;
}
// cout << "hi3" << endl;
bpq=priority_queue<int>(), tpq=priority_queue<int, vector<int>, greater<int>>();
med=bs=ts=0;
// cout << "hi4" << endl;
for(int i=ps.size()-1; i>0; --i) {
upd(ps[i].fi), upd(ps[i].se);
a2=min(a1[i]+ts-(ll)tpq.size()*med+((ll)bpq.size()-1)*med-bs, a2);
}
cout << ba+a2;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'int main()':
bridge.cpp:49:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<ps.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... |