이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 5e5+50;
const int oo = 2e9+7;
const int mod = 1e9+7;
vector<pair<ll,ll>> all;
ll dp[N],p[N],sum[N],B;
ll n ,m,q = 2e5;
ll get(int l, int r) {
return abs(sum[r] - sum[l - 1]);
}
ll min_total_length(vector<int> r,vector<int> b) {
n = r.size();
m = b.size();
for(int i=0;i<n;i++){
all.push_back({r[i],1});
dp[i+1] = oo;
}
for(int i=0;i<m;i++){
all.push_back({b[i],0});
dp[i+n+1] = oo;
}
r.insert(r.begin(),-oo);
r.push_back(oo);
b.insert(b.begin(),-oo);
b.push_back(oo);
n += m;
all.push_back({-oo, -1});
sort(all.begin(),all.end());
memset(p, -1, sizeof(p));
p[q] = 0;
for(int i = 1;i <= n; i++) {
sum[i] = sum[i - 1] + (all[i].second ? -all[i].first : all[i].first);
B += (all[i].second ? -1 : 1);
int j = p[B + q];
p[B + q] = i;
ll nl, nr;
if (all[i].second) {
nl = *(lower_bound(b.begin(), b.end(), all[i].first) - 1);
nr = *lower_bound(b.begin(), b.end(), all[i].first);
}
else {
nl = *(lower_bound(r.begin(),r.end(), all[i].first) - 1);
nr = *lower_bound(r.begin(), r.end(), all[i].first);
}
dp[i] = dp[i - 1] + min(all[i].first - nl, nr - all[i].first);
if(j == -1) continue;
dp[i] = min(dp[i], dp[j] + get(j + 1, i));
}
return dp[n];
}
# | 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... |