This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
#include "wiring.h"
const int maxn = 100010;
vector <ll> dp[maxn];
ll pre[maxn],suf[maxn];
vector <pii> pos;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll tmp = 1e16;
pos.pb({0,-1});
rep(i,0,SZ(r)) pos.pb({r[i],0});
rep(i,0,SZ(b)) pos.pb({b[i],1});
sort(ALL(pos));
int cnt = 0;
rep(i,1,SZ(pos)) if (pos[i].se!=pos[i-1].se) cnt++;
int cur = 1;
rep(i,1,cnt+1) {
int p = cur;
ll presum = 0;
while (p<SZ(pos) and pos[p].se==pos[cur].se) presum += pos[p].fi,p++;
ll sum = 0;
dp[i].resize(p-cur+1);
if (i==1) {
rep(j,0,p-cur) dp[i][j] = tmp;
dp[i][p-cur] = -presum;
}
else {
rep(j,0,SZ(dp[i-1])) {
if (j==0) pre[j] = dp[i-1][j] + 1ll*j*pos[cur-1].fi;
else pre[j] = min(pre[j-1],dp[i-1][j] + 1ll*j*pos[cur-1].fi);
}
for (int j = SZ(dp[i-1])-1;j>=0;j--) {
if (j==SZ(dp[i-1])-1) suf[j] = dp[i-1][j] + 1ll*j*pos[cur].fi;
else suf[j] = min(suf[j+1],dp[i-1][j] + 1ll*j*pos[cur].fi);
}
rep(j,0,p-cur+1) {
dp[i][j] = tmp;
dp[i][j] = min(dp[i][j],pre[min(SZ(dp[i-1])-1,p-cur-j)] + presum - sum - 1ll*(p-cur-j)*pos[cur-1].fi);
if (p-cur-j<SZ(dp[i-1]))dp[i][j] = min(dp[i][j],suf[p-cur-j] + presum - sum + 1ll*(-p+cur+j)*pos[cur].fi);
sum += pos[p-1-j].fi;
presum -= pos[p-1-j].fi;
// cout<<i<<' '<<j<<' '<<dp[i][j]<<endl;
}
}
cur = p;
}
return dp[cnt][0];
}
# | 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... |