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>
#include "wiring.h"
using namespace std;
typedef long long lld;
typedef pair<int, int> pii;
vector<pii> X;
vector<pii> Y;
lld S[200005];
int con[200001];
int lst[400002];
lld D[200002];
long long min_total_length(vector<int> A, vector<int> B) {
int N = A.size(), M = B.size();
int K = N+M;
int i = 0, j = 0;
while (i+j < K){
if (j >= M || (i < N && A[i] <= B[j])){
X.push_back(pii(A[i], 0));
if (j >= M) Y.push_back(pii(abs(A[i] - B.back()), M-1));
else Y.push_back(min(pii(abs(A[i]-B[(M+j-1)%M]), j-1), pii(abs(A[i]-B[j]), j)));
i++;
}
else{
X.push_back(pii(B[j], 1));
if (i >= N) Y.push_back(pii(abs(B[j] - A.back()), N-1));
else Y.push_back(min(pii(abs(A[(N+i-1)%N]-B[j]), i-1), pii(abs(A[i]-B[j]), i)));
j++;
}
}
int cur = 0;
for (int i=0; i<K; i++) con[i] = -2;
for (int i=0; i<K+K+1; i++) lst[i] = -2;
lst[K] = -1;
for (int i=0; i<K; i++){
if (X[i].second == 1){
cur++;
S[i] = X[i].first;
}
else{
cur--;
S[i] = -X[i].first;
}
if (i > 0) S[i] += S[i-1];
if (lst[cur+K] >= -1) con[i] = lst[cur+K];
lst[cur+K] = i;
}
for (int i=0; i<K; i++){
D[i] = D[(i+K)%(K+1)] + Y[i].first;
if (con[i] >= -1){
int j = con[i];
D[i] = min(D[i], D[((j+K+1) % (K+1))] + abs(S[i]-S[j]));
}
}
return D[K-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... |