이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include "wiring.h"
void z(ll &x, ll y){ x = min(x, y); }
const ll INF = 1e18;
ll min_total_length(vector<int> R, vector<int> B){
int n = R.size() + B.size(), pR = 0, pB = 0;
ll pos[n], dp[n]; bool a[n];
for(int i=0; i<n; ++i){
a[i] = pB == (int)B.size() || (pR < (int)R.size() && R[pR] < B[pB]);
pos[i] = a[i] ? R[pR++] : B[pB++];
dp[i] = INF;
}
for(int l=0, r, k=0; l<n; ++l){
if(!l || a[l] == a[l-1]) continue;
for(r=l; r<n && a[l]==a[r]; ++r);
ll preL = 0, sumL = 0, best = l-1 ? dp[l-2] : 0LL;
ll preR = pos[l] - pos[l-1], sumR = preR;
for(int i=l; i<r; ++i){
z(dp[i], best + sumR);
if(i+1<r){
preR += pos[i+1] - pos[i];
sumR += preR;
}
if(k <= l-(i-l)-2){
preL += pos[l-(i-l)-1] - pos[l-(i-l)-2];
sumL += preL;
z(best, (l-(i-l)-3 >= 0 ? dp[l-(i-l)-3] : 0LL) + sumL);
}
}
preL = 0, sumL = 0;
preR -= pos[l] - pos[l-1], sumR -= (pos[l] - pos[l-1]) * (r-l);
int p = r;
for(int i=l-1; i>=k; --i){
preL += pos[i+1] - pos[i];
sumL += preL;
}
best = (k ? dp[k-1] : 0LL) + sumL;
for(int i=k; i<l; ++i){
while(p-l > l-i){
--p;
sumR -= preR;
preR -= pos[p] - pos[p-1];
}
z(dp[p-1], best + sumR);
sumL -= preL;
preL -= pos[i+1] - pos[i];
z(best, dp[i] + sumL);
}
k = l;
for(int i=r-2; i>=l-1; --i) z(dp[i], dp[i+1]);
}
return dp[n-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... |