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;
#define vi vector<int>
#define ll long long
const int maxn = 2e5 + 5;
const ll inf = 1e9;
int n,m,sz;
int pos[maxn];
ll num[maxn], sum[maxn];
ll dp[maxn];
map<int,int> id;
ll get(int l1, int r1, int l2, int r2) {
ll res = (sum[r2]-sum[l2-1]) - (sum[r1]-sum[l1-1]);
res += max(0LL,(num[r2]-num[l2-1])-(num[r1]-num[l1-1]))*(-(sum[r1]-sum[r1-1]));
res += max(0LL,(num[r1]-num[l1-1])-(num[r2]-num[l2-1]))*(sum[l2]-sum[l2-1]);
// printf("----- get (%d, %d) (%d, %d) = %lld\n",l1,r1,l2,r2,res);
return res;
}
void cpidx(vi a, vi b) {
for(auto x : a) id[x];
for(auto x : b) id[x];
for(auto &t : id) t.second = ++sz;
}
ll min_total_length(vi a, vi b) {
n = a.size(); m = b.size();
cpidx(a,b);
for(auto x : a) pos[id[x]] = 0, num[id[x]] = 1, sum[id[x]] = x;
for(auto x : b) pos[id[x]] = 1, num[id[x]] = 1, sum[id[x]] = x;
for(int i=1;i<=sz;i++) num[i] += num[i-1], sum[i] += sum[i-1];
for(int x=sz;x>=1;x--) {
dp[x] = inf;
int i = -1;
ll cur = dp[x+1];
for(int y=x+1;y<=sz;y++) {
// printf("\tx = %d y = %d\n",x,y);
if(pos[y]!=pos[y-1]) {
if(i==-1) i = y;
else break;
}
cur = min(cur, dp[y+1]);
if(i!=-1) {
dp[x] = min(dp[x], get(x,i-1,i,y) + cur);
}
}
// printf("dp %d = %lld\n",x,dp[x]);
}
return dp[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... |