이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cassert>
#include <cstdio>
#include <vector>
#include <climits>
#include <stdlib.h>
#include <algorithm>
using namespace std;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size(), m = b.size();
vector<pair<int,int>> a(n+m);
for(int i=0, j=0; i<n || j<m;)
i==n? a[i+j++]=make_pair(j,1): j==m? a[j+i++]=make_pair(i,0): r[i]<b[j]? a[j+i++]=make_pair(i,0): a[i+j++]=make_pair(j,1);
vector<long long> dp(n+m,LLONG_MAX);
long long sumL=0, cntL=0, sumR=0, cntR=0, gap=0;
int prev=-1, curr=0, optIdx=0;
for(int i=1; i<n+m; i++) {
if (a[i].second != a[i-1].second) {
prev=curr; curr=i; sumR=cntR=0; sumL=0; optIdx=curr-1;
gap= a[prev].second==0? b[a[curr].first]-r[a[curr-1].first]: r[a[curr].first]-b[a[curr-1].first];
}
if (prev==-1) continue;
sumR += a[curr].second==0? r[a[i].first]-r[a[curr].first]:b[a[i].first]-b[a[curr].first];
cntR++;
for(int j=optIdx; j>=prev; j--) {
cntL=curr-j;
sumL += a[prev].second==0? r[a[curr-1].first]-r[a[j].first]:b[a[curr-1].first]-b[a[j].first];
long long temp = sumL + sumR + max(cntL,cntR)*gap;
if (j==0) {dp[i] = min(dp[i],temp); optIdx = 0;}
else {
long long minDP = min(dp[j-1],dp[j]);
if (minDP == LLONG_MAX) continue;
else temp += minDP;
if (dp[i]>temp) {dp[i]=temp;optIdx=j;}
else break;
}
}
for(int k=max(optIdx-1,prev); k<=optIdx; k++)
sumL -= a[prev].second==0? r[a[curr-1].first]-r[a[k].first]:b[a[curr-1].first]-b[a[k].first];
}
return dp[n+m-1];
}
# | 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... |