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>
using namespace std;
#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
const ll inf = 1e18 + 1;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
pair < ll , int > T[N];
ll L[N],R[N],t,dp[N],p[N];
ll bld(ll l, ll r, ll x, ll y){
ll i,sz,p=0;
for(sz=1,i=r; i>=l ;i--, sz++){
p += T[i].st;
L[sz] = min(dp[i],dp[i-1]) - p + sz*x;
R[sz] = min(dp[i],dp[i-1]) - p + sz*y;
}
if(L[sz] < mod || R[sz] < mod) assert(0);
return sz-1;
}
ll min_total_length(vector < int > a, vector < int > b){
int n,m,i,j;
n = a.size(); m = b.size();
for(i=j=0; i<n || j<m ;){
if(j == m || (i < n && a[i]<b[j])) T[++t] = mp(a[i++],1);
else T[++t] = mp(b[j++],2);
}
ll l,r,x,y,sz,mn,k;
memset(L , 22 , sizeof L);
memset(R , 22 , sizeof R);
for(i=1;T[i].nd == T[i+1].nd ; i++) dp[i] = inf;
l = 1; r = i;
dp[i] = inf;
for(i++;i<=t;i = r+1){
for(j=i ;T[j].nd == T[j+1].nd ; j++);
x = T[i].st;
y = T[i-1].st;
ll psz = bld(l,r,x,y);
mn = inf;
for(sz=1,k=i;k<=j;k++,sz++){
p[sz] = p[sz-1] + T[k].st;
mn = min(mn , R[sz]);
dp[k] = mn + p[sz] - sz*y;
}
mn = inf;
for(sz=j-i+1;psz>sz;psz--) mn = min(mn , L[psz]);
for(sz=j-i+1,k=j; k>=i ;k--,sz--){
mn = min(mn , L[sz]);
dp[k] = min(dp[k] , mn + p[sz] - sz*x);
}
for(k=1;k<=r-l+1;k++) L[k] = R[k] = inf;
l = i; r = j;
}
return dp[t];
}
//int main(){
// cout << min_total_length({3, 6, 7}, {0, 4, 5, 9, 10});
//}
# | 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... |