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<bits/stdc++.h>
using namespace std;
#define for2(a,b,c) for(int a=b;a<c;a++)
#define ll long long
#include "wiring.h"
ll sub2(vector<int> r,vector<int> b){
ll res = 0;
int L = 0;
int R = b.size()-1;
while(L != r.size() && R != -1){
res += b[R]-r[L];
L++;
R--;
}
while(R != -1){
res += b[R]-r[L-1];
R--;
}
while(L != r.size()){
res += b[R+1]-r[L];
L++;
}
return res;
}
#define PII pair<int,int>
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
const int maxn = 200100;
ll dp[maxn];
ll sub3(vector<int> v1,vector<int> v2){
vector<int> v[2];
vector<PII> V;
vector<int> par[2];
vector<ll> jam[2];
for(auto x : v1) {
v[0].pb(x);
V.pb({x,0});
}
for(auto x : v2){
v[1].pb(x);
V.pb({x,1});
}
sort(all(V));
int n = V.size();
par[0].resize(n+1);
par[1].resize(n+1);
jam[0].resize(n+1);
jam[1].resize(n+1);
for2(i,0,n){
par[0][i+1] = par[0][i] + (V[i].S==0);
par[1][i+1] = par[1][i] + (V[i].S==1);
jam[0][i+1] = jam[0][i] + V[i].F;
/// jam[1][i+1] = jam[1][i] +
}
for2(i,0,n){
int x = V[i].F;
bool t = V[i].S;
dp[i] = 1e18;
ll tmp = 1e18;
int ind = lower_bound(all(v[!t]),x)-v[!t].begin();
if(ind != v[!t].size()) tmp = min(tmp,(ll)v[!t][ind]-x);
ind --;
if(ind >= 0) tmp = min(tmp,(ll)x-v[!t][ind]);
if(i) tmp += dp[i-1];
dp[i] = tmp;
int st = -1;
int en = i;
while(st != en-1){
int mid = (st+en)/2;
if(par[!t][i+1]-par[!t][mid] == 0) en = mid;
else st = mid;
}
int cnt = i-en+1;
if(en-cnt < 0 || par[t][en]-par[t][en-cnt]) continue;
tmp = 0;
ll k = 0;
/* for(int j = i; j >= i-cnt; j--){
if(j != i) tmp += k*(V[j+1].F-V[j].F);
k++;
}*/
tmp = jam[0][i+1]-jam[0][i+1-cnt];
tmp -= (ll)V[i-cnt].F*cnt;
k = 0;
for(int j = i-cnt*2+1; j <= i-cnt; j++){
if(j != i-2*cnt+1) tmp += k*(V[j].F-V[j-1].F);
k++;
}
dp[i] = min(dp[i],dp[i-2*cnt]+tmp);
}
return dp[V.size()-1];
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if(r.back() < b[0]) return sub2(r,b);
return sub3(r,b);
}
Compilation message (stderr)
wiring.cpp: In function 'long long int sub2(std::vector<int>, std::vector<int>)':
wiring.cpp:11:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(L != r.size() && R != -1){
~~^~~~~~~~~~~
wiring.cpp:20:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(L != r.size()){
~~^~~~~~~~~~~
wiring.cpp: In function 'long long int sub3(std::vector<int>, std::vector<int>)':
wiring.cpp:69:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ind != v[!t].size()) tmp = min(tmp,(ll)v[!t][ind]-x);
~~~~^~~~~~~~~~~~~~~
# | 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... |