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<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
const int TREE_SIZE=1<<20;
ll tree[2*TREE_SIZE];
ll min_pre[2*TREE_SIZE];
void add(int i,ll x){
int node=TREE_SIZE+i;
tree[node]+=x;
min_pre[node]+=x;
node/=2;
while(node){
tree[node]=tree[2*node]+tree[2*node+1];
min_pre[node]=min(min(min_pre[2*node],tree[2*node]),min_pre[2*node+1]+tree[2*node]);
node/=2;
}
}
ll get_min(int node,int l,int r,int rl,int rr){
if(r<=rl||rr<=l)
return 1e17;
if(l<=rl&&rr<=r)
return min_pre[node];
int mid=(rl+rr)/2;
return min(get_min(2*node,l,r,rl,mid),tree[2*node]+get_min(2*node+1,l,r,mid,rr));
}
ll get_min(int l,int r){
return get_min(1,l,r,0,TREE_SIZE);
}
ll min_total_length(vector<int>r,vector<int>b){
vector<pair<int,int>>arr;
for(int i:r)
arr.push_back({i,0});
for(int i:b)
arr.push_back({i,1});
sort(arr.begin(),arr.end());
vector<ll>dp(arr.size(),1e17);
dp.push_back(0);
arr.emplace_back(-1,arr.back().second^1);
int i2=(int)dp.size()-1;
int i3=(int)dp.size()-1;
multiset<int>ms;
ll cost1=0;
for(int i1=(int)dp.size()-2;i1>=0;i1--){
if(arr[i2].second==arr[i1].second){
i3=i2;
i2=i1+1;
cost1=0;
int first1=arr[i2-1].first;
for(int i=i2;i<i3;i++){
add(i,arr[i].first-first1);
add(i,dp[i+1]);
add(i+1,-dp[i+1]);
}
}
if(i2==dp.size()-1)
continue;
int mid=arr[i2].first;
int num1=i2-i1;
int first1=arr[i2-1].first;
cost1+=mid-arr[i1].first;
add(2*i2-i1-1,first1-mid);
dp[i1]=min(dp[i2],get_min(i2,i3))+cost1;
}
return dp[0];
}
Compilation message (stderr)
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:69:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if(i2==dp.size()-1)
| ~~^~~~~~~~~~~~~
wiring.cpp:73:13: warning: unused variable 'num1' [-Wunused-variable]
73 | int num1=i2-i1;
| ^~~~
# | 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... |