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 "wiring.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<utility>
using namespace std;
#define Int long long
#define rep(i,a,b) for(Int i=a; i<(Int)b; i++)
#define V vector
#define nl '\n'
#define _ << " " <<
#define dbg(x) cout << "?" << #x << " : " << x << endl;
const Int INF = 1e18;
const Int N = 2e5+5;
Int n, m;
V<Int> a, t;
Int prf[N];
Int rgsum(Int l,Int r){
return prf[r] - prf[l-1];
}
Int dp[N];
//
Int b[N], c[N];
Int min_total_length(V<int> red,V<int> blue){
n = red.size(); m = blue.size();
a = t = {-1};
{
Int i=0, j=0;
while(i<n && j<m){
if(red[i] < blue[j]){
a.push_back(red[i++]); t.push_back(0);
}
else{
a.push_back(blue[j++]); t.push_back(1);
}
}
while(i<n){
a.push_back(red[i++]); t.push_back(0);
}
while(j<m){
a.push_back(blue[j++]); t.push_back(1);
}
}
// cout << "a[] ";
rep(i,1,n+m+1){
prf[i] = prf[i-1] + a[i];
// cout << a[i] << " ";
}
// cout << nl;
dp[0] = 0;
rep(i,1,n+m+1) dp[i] = INF;
for(Int l=1,r=1; l<=n+m; l=r+1){
r = l;
while(r+1<=n+m && t[r+1]==t[l]) r++;
if(l==1) continue;
// cout << "lr" _ l _ r << nl;
Int mxlef = 0;
for(Int j=l-1; t[j]==t[l-1]; j--){
Int lef = l-1 - j + 1;
mxlef = lef;
Int cost = min(dp[j-1],dp[j])
+ lef*a[l-1] - rgsum(j,l-1);
b[lef] = cost + lef*(a[l]-a[l-1]);
c[lef] = cost;
}
for(Int lef=mxlef-1; lef>=1; lef--){
b[lef] = min(b[lef], b[lef+1]);
}
for(Int lef=2; lef<=mxlef; lef++){
c[lef] = min(c[lef], c[lef-1]);
}
for(Int i=l; i<=r; i++){
Int rig = i - l + 1;
dp[i] = min({
mxlef>=rig ? b[rig] + rgsum(l,i) - rig*a[l] : INF,
c[min(mxlef,rig)] + rig*(a[l]-a[l-1]) + rgsum(l,i) - rig*a[l]
});
// cout << i _ dp[i] << nl;
}
// cout << nl;
}
// dp[i] = min({
// dp[i],
// min(dp[j-1],dp[j])
// + lef*a[l-1] - rgsum(j,l-1)
// + max(lef,rig)*(a[l]-a[l-1])
// + rgsum(l,i) - rig*a[l]
// });
Int ans = dp[n+m];
return ans;
}
# | 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... |