이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "wiring.h"
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;
int n, m;
int S[500050], T[500050];
pii p[1000010]; int pz;
ll D[1000010];
ll d2[1000010], temp[1000010];
void Do(vector <ll> &X, vector <ll> &Y) {
int zx = szz(X), zy = szz(Y);
d2[0] = D[0];
for(int i=1;i<=zx;i++) d2[i] = min(d2[i-1] + Y[0] - X[i-1], D[i]);
temp[0] = D[zx];
ll sx = 0, sy = 0;
for(int i=1;i<=zy;i++) {
temp[i] = temp[i-1] + Y[i-1] - X.back();
if(i <= zx) {
sy += Y[i-1];
sx += X[zx - i];
temp[i] = min(temp[i], d2[zx - i] + sy - sx);
}
}
for(int i=0;i<=zy;i++) D[i] = temp[i];
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n = szz(r), m = szz(b);
for(int i=1;i<=n;i++) S[i] = r[i-1];
for(int i=1;i<=m;i++) T[i] = b[i-1];
for(int i=1, j=1;i<=n || j<=m;) {
if(i > n || (j <= m && T[j] < S[i])) p[++pz] = pii(T[j++], 1);
else p[++pz] = pii(S[i++], 0);
}
int f = -1;
for(int i=2;i<=pz;i++) if(p[i].Se != p[i-1].Se) { f = i; break; }
vector <ll> A, B;
for(int i=1;i<f;i++) A.pb(p[i].Fi);
for(int i=1;i<=szz(A);i++) D[i] = 1e18;
for(int i=f;i<=pz;) {
int j = i;
while(j <= pz && p[i].Se == p[j].Se) B.pb(p[j++].Fi);
Do(A, B);
i = j;
swap(A, B); B.clear();
}
return D[szz(A)];
}
# | 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... |