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;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
inline int in() {
int result = 0;
char ch = getchar_unlocked();
while (true) {
if(ch >= '0' && ch <= '9')
break;
ch = getchar_unlocked();
}
result = ch-'0';
while(true) {
ch = getchar_unlocked();
if (ch < '0' || ch > '9')
break;
result = result*10 + (ch - '0');
}
return result;
}
inline void out(ll x) {
ll rev=x;
int count = 0;
if(x == 0) {
putchar_unlocked('0');
return;
}
while((rev % 10) == 0) {
++count;
rev /= 10;
} //obtain the count of the number of 0s
rev = 0;
while(x != 0) {
rev = rev*10 + x % 10;
x /= 10;
} //store reverse of N in rev
while(rev != 0) {
putchar_unlocked(rev % 10 + '0');
rev /= 10;
}
while(count--)
putchar_unlocked('0');
}
inline char rc() {
char c=getchar_unlocked();
while(c!='A'&&c!='B')
c=getchar_unlocked();
return c;
}
const int mxN=1e5;
int k, n, med;
ll ba, a1[mxN+1], a2, 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() {
k=in(), n=in();
for(int i=0; i<n; ++i) {
char z1, z2;
pii pi;
z1=rc(), pi.fi=in(), z2=rc(), pi.se=in();
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;
});
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;
}
a2=a1[ps.size()];
if(k==2) {
bpq=priority_queue<int>(), tpq=priority_queue<int, vector<int>, greater<int>>();
med=bs=ts=0;
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);
}
}
out(ba+a2);
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:92: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... |