# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
146176 | mhy908 | Palembang Bridges (APIO15_bridge) | C++14 | 135 ms | 10724 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
LL temp, ans=987654321987654321;
int n, re, k;
LL arr[200010], sum[200010], arrr[200010], arrrr[200010];
int re2;
pll input[100010];
LL lef, rig, mid;
priority_queue<LL> l, r;
void in_pq(LL a)
{
if(mid<=a){
r.push(-a);
rig+=a;
}
else{
l.push(a);
lef+=a;
}
if(r.size()>=l.size()+2){
LL temp=-r.top();
r.pop();
rig-=temp;
l.push(mid);
lef+=mid;
mid=temp;
}
if(r.size()+2<=l.size()){
LL temp=l.top();
l.pop();
lef-=temp;
r.push(-mid);
rig+=mid;
mid=temp;
}
}
LL solve()
{
return mid*l.size()-lef+rig-mid*r.size();
}
int main()
{
scanf("%d %d", &k, &n);
for(int i=1; i<=n; i++){
char a, b;
LL c, d;
scanf(" %c %lld %c %lld", &a, &c, &b, &d);
if(a==b)temp+=llabs(c-d);
else{
arr[++re]=c;
arr[++re]=d;
input[++re2]={min(c, d), max(c, d)};
}
}
sort(arr+1, arr+re+1);
sort(input+1, input+re2+1, [](const pll& a, const pll& b){return a.F+a.S<b.F+b.S;});
for(int i=1; i<=re; i++){
sum[i]=sum[i-1]+arr[i];
}
if(k==1){
ans=sum[re]-sum[re/2]*2;
printf("%lld", ans+temp+(LL)re/2);
return 0;
}
mid=input[1].F;
in_pq(input[1].S);
arrr[1]=solve();
for(int i=2; i<=re2; i++){
in_pq(input[i].F);
in_pq(input[i].S);
arrr[i]=solve();
}
lef=0;
rig=0;
while(!l.empty())l.pop();
while(!r.empty())r.pop();
mid=input[re2].F;
in_pq(input[re2].S);
arrrr[re2]=solve();
for(int i=re2-1; i>=1; i--){
in_pq(input[i].F);
in_pq(input[i].S);
arrrr[i]+=solve();
}
for(int i=0; i<=re2; i++){
ans=min(ans, arrr[i]+arrrr[i+1]);
}
printf("%lld", ans+temp+(LL)re/2);
}
컴파일 시 표준 에러 (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... |