#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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
36 ms |
8884 KB |
Output is correct |
4 |
Correct |
37 ms |
8868 KB |
Output is correct |
5 |
Correct |
36 ms |
8316 KB |
Output is correct |
6 |
Correct |
45 ms |
10536 KB |
Output is correct |
7 |
Correct |
48 ms |
10676 KB |
Output is correct |
8 |
Correct |
46 ms |
10524 KB |
Output is correct |
9 |
Correct |
46 ms |
10680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
50 ms |
12040 KB |
Output is correct |
4 |
Correct |
49 ms |
11696 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2744 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
52 ms |
12208 KB |
Output is correct |
19 |
Correct |
53 ms |
12112 KB |
Output is correct |
20 |
Correct |
51 ms |
12044 KB |
Output is correct |
21 |
Correct |
56 ms |
12216 KB |
Output is correct |
22 |
Correct |
53 ms |
12056 KB |
Output is correct |
23 |
Correct |
49 ms |
12040 KB |
Output is correct |
24 |
Correct |
50 ms |
11956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
40 ms |
10856 KB |
Output is correct |
3 |
Correct |
51 ms |
11116 KB |
Output is correct |
4 |
Correct |
48 ms |
11116 KB |
Output is correct |
5 |
Correct |
53 ms |
11188 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2752 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2696 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
46 ms |
10680 KB |
Output is correct |
19 |
Correct |
48 ms |
11192 KB |
Output is correct |
20 |
Correct |
41 ms |
10932 KB |
Output is correct |
21 |
Correct |
46 ms |
10676 KB |
Output is correct |
22 |
Correct |
48 ms |
10676 KB |
Output is correct |
23 |
Correct |
43 ms |
10364 KB |
Output is correct |
24 |
Correct |
42 ms |
10540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
36 ms |
8884 KB |
Output is correct |
20 |
Correct |
37 ms |
8868 KB |
Output is correct |
21 |
Correct |
36 ms |
8316 KB |
Output is correct |
22 |
Correct |
45 ms |
10536 KB |
Output is correct |
23 |
Correct |
48 ms |
10676 KB |
Output is correct |
24 |
Correct |
46 ms |
10524 KB |
Output is correct |
25 |
Correct |
46 ms |
10680 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
50 ms |
12040 KB |
Output is correct |
29 |
Correct |
49 ms |
11696 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2744 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
1 ms |
2652 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
1 ms |
2652 KB |
Output is correct |
43 |
Correct |
52 ms |
12208 KB |
Output is correct |
44 |
Correct |
53 ms |
12112 KB |
Output is correct |
45 |
Correct |
51 ms |
12044 KB |
Output is correct |
46 |
Correct |
56 ms |
12216 KB |
Output is correct |
47 |
Correct |
53 ms |
12056 KB |
Output is correct |
48 |
Correct |
49 ms |
12040 KB |
Output is correct |
49 |
Correct |
50 ms |
11956 KB |
Output is correct |
50 |
Correct |
1 ms |
2652 KB |
Output is correct |
51 |
Correct |
40 ms |
10856 KB |
Output is correct |
52 |
Correct |
51 ms |
11116 KB |
Output is correct |
53 |
Correct |
48 ms |
11116 KB |
Output is correct |
54 |
Correct |
53 ms |
11188 KB |
Output is correct |
55 |
Correct |
1 ms |
2652 KB |
Output is correct |
56 |
Correct |
1 ms |
2652 KB |
Output is correct |
57 |
Correct |
1 ms |
2652 KB |
Output is correct |
58 |
Correct |
1 ms |
2752 KB |
Output is correct |
59 |
Correct |
1 ms |
2652 KB |
Output is correct |
60 |
Correct |
1 ms |
2652 KB |
Output is correct |
61 |
Correct |
1 ms |
2696 KB |
Output is correct |
62 |
Correct |
1 ms |
2652 KB |
Output is correct |
63 |
Correct |
1 ms |
2648 KB |
Output is correct |
64 |
Correct |
1 ms |
2652 KB |
Output is correct |
65 |
Correct |
1 ms |
2652 KB |
Output is correct |
66 |
Correct |
1 ms |
2652 KB |
Output is correct |
67 |
Correct |
46 ms |
10680 KB |
Output is correct |
68 |
Correct |
48 ms |
11192 KB |
Output is correct |
69 |
Correct |
41 ms |
10932 KB |
Output is correct |
70 |
Correct |
46 ms |
10676 KB |
Output is correct |
71 |
Correct |
48 ms |
10676 KB |
Output is correct |
72 |
Correct |
43 ms |
10364 KB |
Output is correct |
73 |
Correct |
42 ms |
10540 KB |
Output is correct |
74 |
Correct |
50 ms |
11284 KB |
Output is correct |
75 |
Correct |
46 ms |
10932 KB |
Output is correct |
76 |
Correct |
50 ms |
11292 KB |
Output is correct |
77 |
Correct |
68 ms |
10856 KB |
Output is correct |
78 |
Correct |
45 ms |
10708 KB |
Output is correct |
79 |
Correct |
41 ms |
10680 KB |
Output is correct |
80 |
Correct |
46 ms |
10676 KB |
Output is correct |
81 |
Correct |
46 ms |
10920 KB |
Output is correct |
82 |
Correct |
43 ms |
10688 KB |
Output is correct |
83 |
Correct |
47 ms |
10596 KB |
Output is correct |
84 |
Correct |
48 ms |
10936 KB |
Output is correct |
85 |
Correct |
44 ms |
11444 KB |
Output is correct |
86 |
Correct |
50 ms |
11276 KB |
Output is correct |
87 |
Correct |
48 ms |
11080 KB |
Output is correct |
88 |
Correct |
49 ms |
11448 KB |
Output is correct |
89 |
Correct |
49 ms |
11388 KB |
Output is correct |
90 |
Correct |
49 ms |
11436 KB |
Output is correct |
91 |
Correct |
47 ms |
11448 KB |
Output is correct |
92 |
Correct |
68 ms |
11296 KB |
Output is correct |
93 |
Correct |
50 ms |
11284 KB |
Output is correct |
94 |
Correct |
48 ms |
11444 KB |
Output is correct |
95 |
Correct |
51 ms |
11444 KB |
Output is correct |
96 |
Correct |
50 ms |
11192 KB |
Output is correct |
97 |
Correct |
44 ms |
11472 KB |
Output is correct |
98 |
Correct |
44 ms |
11192 KB |
Output is correct |
99 |
Correct |
43 ms |
11188 KB |
Output is correct |
100 |
Correct |
47 ms |
11352 KB |
Output is correct |
101 |
Correct |
50 ms |
11532 KB |
Output is correct |
102 |
Correct |
49 ms |
11448 KB |
Output is correct |