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 <bits/stdc++.h>
#define ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const long long inf=1e18;
const ll mod=1e9+7;
const ll N=3e5+7;
const ld eps=1e-9;
long long dp[N];
vll r,b,v;
ll c[N];
long long pref[N],suf[N];
long long dis(ll x,ll y){
return abs(v[x]-v[y]);
}
long long min_total_length(vector<int> R, vector<int> B) {
ll i;
//setting up stuff
r=R,b=B;
for(auto i : r) v.pb(i);
for(auto i : b) v.pb(i);
r.pb(-1);
b.pb(-1);
v.pb(-1);
sort(all(r));
sort(all(b));
sort(all(v));
deque<pair<ll,ll>> seg;
for(i=1;i<(ll)v.sz;i++){
ll low=lb(all(r), v[i]) - r.begin();
if(low < (ll)r.sz && r[low] == v[i]) c[i] = 1;
if(seg.sz == 0 || c[i] != c[i - 1])seg.pb({i, i});
else seg.bc.sc=i;
}
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
while(seg.sz>1){
ll a=seg[0].fr,b=seg[0].sc,c=seg[1].fr,d=seg[1].sc;
suf[b]=0;
for(i=b-1;i>=a;i--){
suf[i]=suf[i+1]+dis(i,b);
}
for(i=a;i<=b;i++){
suf[i]+=min(dp[i],dp[i-1]);
pref[i]=(b-i+1)*dis(b,c)+suf[i];
if(i>a)chmin(pref[i],pref[i-1]);
}
for(i=b-1;i>=a;i--)chmin(suf[i],suf[i+1]);
pref[c]=0;
for(i=c;i<=d;i++){
if(i>c)pref[i]=pref[i-1]+dis(i,c);
dp[i] = pref[i] + suf[max(a, b - (i - c))] + dis(b, c) * (i - c + 1);
if(b - (i - c) - 1 >= a) chmin(dp[i], pref[i] + pref[b - (i - c) - 1]);
}
seg.pof;
}
return dp[v.sz-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... |