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>
using namespace std;
//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e18
#define pb push_back
#define fi first
#define se second
#define sz size()
const int MX = 6e5;
priority_queue<ii, vii, greater<ii>> pq;
int n[2], p[2], m=0;
vi f[2];
ll a[MX], b[MX];
ll dp[MX], ch[MX];
ll ans=0;
ll getCost(ll bg, ll ed) {
if(ch[ed]-ch[bg] != 1) return INF;
ll lb=bg+1, ub=ed;
while(lb != ub) {
ll mid=(lb+ub)/2;
if(b[mid] == b[bg]) lb=mid+1;
else ub=mid;
}
ll ret = 0;
REP(i,bg,lb ) ret += a[lb-1] - a[i ];
REP(i,lb,ed+1) ret += a[i ] - a[lb];
ret += (a[lb]-a[lb-1])*max(lb-bg,ed-lb+1);
return ret;
}
ll min_total_length(std::vector<int> R, std::vector<int> B) {
f[0] = R; f[1] = B;
RE(i,2) n[i] = f[i].sz;
// fill a and b
RE(i,2) RE(j,n[i]) pq.push({f[i][j], i});
while(!pq.empty()) {
a[m] = pq.top().fi;
b[m] = pq.top().se;
pq.pop(); m++;
}
ch[0]=0;
REP(i,1,m) {
ch[i] = ch[i-1];
if(b[i] != b[i-1]) ch[i]++;
}
dp[0] = 0;
REP(i,1,m+1) {
ll mn=INF;
dp[i] = INF;
REV(j,0,i) {
mn = min(mn, dp[j]);
dp[i] = min(dp[i], mn + getCost(j,i-1));
}
}
return dp[m];
}
# | 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... |