#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;
const long long maxn=200000+10;
long long dp[maxn],inf=1e16,vas[maxn];
long long n,m,ps[maxn],suf[maxn];
vector<long long>allind;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n=(long long)r.size();
m=(long long)b.size();
allind.push_back(-1);
for(auto x:r){
allind.push_back(x);
}
for(auto x:b){
allind.push_back(x);
}
sort(allind.begin(),allind.end());
allind.resize(unique(allind.begin(),allind.end())-allind.begin());
for(long long i=0;i<n;i++){
r[i]=lower_bound(allind.begin(),allind.end(),r[i])-allind.begin();
vas[r[i]]=1;
}
for(long long i=0;i<m;i++){
b[i]=lower_bound(allind.begin(),allind.end(),b[i])-allind.begin();
vas[b[i]]=2;
}
for(long long i=1;i<maxn;i++){
dp[i]=inf;
if((int)allind.size()>i)
ps[i]=ps[i-1]+allind[i];
}
for(int i=(int)allind.size()-1;i>=1;i--){
suf[i]=suf[i+1]+allind[i];
}
dp[0]=0;
long long sz=(long long)allind.size();
if(sz!=n+m+1){
exit(23);
}
for(long long i=1;i<sz;i++){
long long av=allind[i],unnow=0,last=i,bar=0,akh;
for(long long j=i-1;j>=1;j--){
if(vas[j]==vas[i]&&vas[j]!=vas[j+1]){
break;
}
if(vas[j]==vas[i]){
last=j;
unnow+=av;
av=allind[j];
}else{
bar+=allind[j];
akh=allind[last-1];
long long ted1=i-last+1;
long long ted2=(last-1)-j+1;
dp[i]=min(dp[i],min(dp[j],dp[j-1])+(ps[i]-ps[last-1])-(allind[last]*ted1)-(suf[j]-suf[last])+(allind[last-1]*ted2)+abs(ted2+ted1)*(allind[last]-allind[last-1]));
}
}
// cout<<i<<" "<<dp[i]<<" "<<vas[i]<<endl;
}
return dp[sz-1];
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:43:47: warning: variable 'akh' set but not used [-Wunused-but-set-variable]
43 | long long av=allind[i],unnow=0,last=i,bar=0,akh;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3932 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '45513' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3832 KB |
3rd lines differ - on the 1st token, expected: '904', found: '949' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3928 KB |
3rd lines differ - on the 1st token, expected: '316', found: '364' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3928 KB |
3rd lines differ - on the 1st token, expected: '27', found: '34' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3932 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '45513' |
2 |
Halted |
0 ms |
0 KB |
- |