# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
640803 | Son | Palembang Bridges (APIO15_bridge) | C++14 | 89 ms | 6724 KiB |
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 pb push_back
#define mp make_pair
#define fi first
#define se second
int K,N;
char Ax[10], Bx[10];
int A[100005], B[100005];
vector < int > V;
LL lsum = 0, rsum = 0;
LL P[100005], S[100005];
priority_queue < int > lpq;
priority_queue < int , vector < int > , greater < int > > rpq;
bool cmp( int a, int b ){
return A[a] + B[a] < A[b] + B[b];
}
void reset(){
lsum = rsum = 0;
while ( !lpq.empty() ) lpq.pop();
while ( !rpq.empty() ) rpq.pop();
}
void add( int x ){
if ( lpq.empty() || x <= lpq.top() ){
lpq.push(x);
lsum += x;
} else {
rpq.push(x);
rsum += x;
}
if ( rpq.size() + 1 < lpq.size() ){
int y = lpq.top();
lpq.pop();
rpq.push(y);
lsum -= y;
rsum += y;
} else if ( lpq.size() < rpq.size() ){
int y = rpq.top();
rpq.pop();
lpq.push(y);
rsum -= y;
lsum += y;
}
}
int main(){
LL ss = 0, bridges = 0;
scanf("%d%d",&K,&N);
for ( int i = 1; i <= N; i++ ){
scanf("%s%d%s%d",&Ax,&A[i],&Bx,&B[i]);
if ( A[i] > B[i] ){
swap(A[i],B[i]);
}
if ( Ax[0] == Bx[0] ){
ss += B[i] - A[i];
} else {
bridges += 1;
V.push_back(i);
}
}
sort(V.begin(),V.end(),cmp);
reset();
for ( int i = 0; i < V.size(); i++ ){
int id = V[i];
add(A[id]);
add(B[id]);
P[i] = rsum - lsum;
}
reset();
for ( int i = V.size()-1; i >= 0; i-- ){
int id = V[i];
add(A[id]);
add(B[id]);
S[i] = rsum - lsum;
}
LL ans = P[V.size()-1] + ss + bridges;
for ( int i = 0; i < V.size() && K == 2; i++ ){
LL temp = ss + bridges + S[i];
if ( i ) temp += P[i-1];
if ( temp < ans ) ans = temp;
}
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
# | 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... |