# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
25325 | tlwpdus | Palembang Bridges (APIO15_bridge) | C++14 | 139 ms | 5824 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;
typedef long long ll;
typedef pair<int,int> pii;
struct median {
int sz;
ll sum1, sum2;
priority_queue<int> pq1;
priority_queue<int,vector<int>,greater<int> > pq2;
median() {sz = 0; sum1 = sum2 = 0;}
void cle() {
sz = 0; sum1 = sum2 = 0;
while(!pq1.empty()) pq1.pop();
while(!pq2.empty()) pq2.pop();
}
void push(int a) {
if (sz&1) {
if (a<pq1.top()) {
pq1.push(a);
sum1 += a;
pq2.push(pq1.top());
sum2 += pq1.top();
sum1 -= pq1.top();
pq1.pop();
}
else {
sum2+=a;
pq2.push(a);
}
}
else {
if (pq1.empty()||a<pq1.top()) {
sum1+=a;
pq1.push(a);
}
else {
pq2.push(a);
sum2+=a;
pq1.push(pq2.top());
sum1+=pq2.top();
sum2-=pq2.top();
pq2.pop();
}
}
sz++;
}
ll getmed() {
if (pq1.empty()) return 0;
ll med = pq1.top();
return ((int)pq1.size()-(int)pq2.size())*med-sum1+sum2;
}
} md;
int k, n, p;
pii arr[100100];
ll val[2][100100];
ll res;
bool cmp(pii a, pii b) {return a.first+a.second<b.first+b.second;}
int main() {
int i;
//freopen("input","r",stdin);
scanf("%d%d",&k,&n);
for (i=0;i<n;i++) {
char a[3], c[3]; int b, d;
scanf("%s%d%s%d",a,&b,c,&d);
if (a[0]==c[0]) res += abs(d-b);
else {
arr[p++] = pii(b,d);
res++;
}
}
if (k==1) {
for (i=0;i<p;i++) {
md.push(arr[i].first);
md.push(arr[i].second);
}
printf("%lld\n",res+md.getmed());
return 0;
}
sort(arr,arr+p,cmp);
for (i=0;i<p;i++) {
md.push(arr[i].first);
md.push(arr[i].second);
val[0][i+1] = md.getmed();
}
md.cle();
for (i=p-1;i>=0;i--) {
val[1][i+1] = md.getmed();
md.push(arr[i].first);
md.push(arr[i].second);
}
val[1][0] = md.getmed();
ll mini = (1LL<<60);
for (i=0;i<=p;i++) mini = min(mini,val[0][i]+val[1][i]);
printf("%lld\n",res+mini);
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... |