#include "wiring.h"
#include <bits/stdc++.h>
#define N 100005
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll,int>P;
int n,m,k;
P c[N*2];
ll dp[2*N],diff[N],mdist[N];
int plc[N],appear[2*N],amt,p[2];
ll min_total_length(vector<int> a,vector<int> b) {
n=a.size(),m=b.size(),k=n+m;
for(int i=0,j=0;i+j<k;){
if(j>=m||i<n&&a[i]<b[j]){
c[i+j+1]={(ll)a[i],0};
i++;
}
else{
c[i+j+1]={(ll)b[j],1};
j++;
}
}
for(int i=1;i<=k;i++)
mdist[i]=1e9+10;
p[0]=p[1]=0;
for(int i=1;i<=k;i++){
if(p[!c[i].s])
mdist[i]=min(mdist[i],abs(c[i].f-c[p[!c[i].s]].f));
p[c[i].s]=i;
}
p[0]=p[1]=0;
for(int i=k;i>=1;i--){
if(p[!c[i].s])
mdist[i]=min(mdist[i],abs(c[p[!c[i].s]].f-c[i].f));
p[c[i].s]=i;
}
for(int i=0;i<=2*k;i++)
appear[i]=-1;
appear[k]=0;
for(int i=1;i<=k;i++){
plc[i]=-1;
if(c[i].s){
amt++;
diff[i]=diff[i-1]+c[i].f;
}
else{
amt--;
diff[i]=diff[i-1]-c[i].f;
}
if(appear[amt+k]!=-1)
plc[i]=appear[amt+k];
appear[amt+k]=i;
}
for(int i=1;i<=k;i++){
dp[i]=dp[i-1]+mdist[i];
if(plc[i]!=-1)
dp[i]=min(dp[i],dp[plc[i]]+abs(diff[i]-diff[plc[i]]));
}
return dp[k];
}
Compilation message
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:16:21: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if(j>=m||i<n&&a[i]<b[j]){
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9440 KB |
Output is correct |
2 |
Correct |
0 ms |
9440 KB |
Output is correct |
3 |
Correct |
0 ms |
9440 KB |
Output is correct |
4 |
Correct |
0 ms |
9440 KB |
Output is correct |
5 |
Correct |
0 ms |
9440 KB |
Output is correct |
6 |
Correct |
0 ms |
9440 KB |
Output is correct |
7 |
Correct |
0 ms |
9440 KB |
Output is correct |
8 |
Correct |
0 ms |
9440 KB |
Output is correct |
9 |
Correct |
0 ms |
9440 KB |
Output is correct |
10 |
Correct |
0 ms |
9440 KB |
Output is correct |
11 |
Correct |
0 ms |
9440 KB |
Output is correct |
12 |
Correct |
0 ms |
9440 KB |
Output is correct |
13 |
Correct |
0 ms |
9440 KB |
Output is correct |
14 |
Correct |
0 ms |
9440 KB |
Output is correct |
15 |
Correct |
0 ms |
9440 KB |
Output is correct |
16 |
Correct |
0 ms |
9440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9440 KB |
Output is correct |
2 |
Correct |
0 ms |
9440 KB |
Output is correct |
3 |
Incorrect |
36 ms |
10616 KB |
3rd lines differ - on the 1st token, expected: '41596985758595', found: '-8897360094202480780' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9440 KB |
Output is correct |
2 |
Correct |
0 ms |
9440 KB |
Output is correct |
3 |
Incorrect |
43 ms |
11008 KB |
3rd lines differ - on the 1st token, expected: '1068938599', found: '-359014890010758' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9440 KB |
Output is correct |
2 |
Incorrect |
46 ms |
11008 KB |
3rd lines differ - on the 1st token, expected: '373710605', found: '-24727760454836' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9440 KB |
Output is correct |
2 |
Correct |
0 ms |
9440 KB |
Output is correct |
3 |
Correct |
0 ms |
9440 KB |
Output is correct |
4 |
Correct |
0 ms |
9440 KB |
Output is correct |
5 |
Correct |
0 ms |
9440 KB |
Output is correct |
6 |
Correct |
0 ms |
9440 KB |
Output is correct |
7 |
Correct |
0 ms |
9440 KB |
Output is correct |
8 |
Correct |
0 ms |
9440 KB |
Output is correct |
9 |
Correct |
0 ms |
9440 KB |
Output is correct |
10 |
Correct |
0 ms |
9440 KB |
Output is correct |
11 |
Correct |
0 ms |
9440 KB |
Output is correct |
12 |
Correct |
0 ms |
9440 KB |
Output is correct |
13 |
Correct |
0 ms |
9440 KB |
Output is correct |
14 |
Correct |
0 ms |
9440 KB |
Output is correct |
15 |
Correct |
0 ms |
9440 KB |
Output is correct |
16 |
Correct |
0 ms |
9440 KB |
Output is correct |
17 |
Correct |
0 ms |
9440 KB |
Output is correct |
18 |
Correct |
0 ms |
9440 KB |
Output is correct |
19 |
Incorrect |
36 ms |
10616 KB |
3rd lines differ - on the 1st token, expected: '41596985758595', found: '-8897360094202480780' |
20 |
Halted |
0 ms |
0 KB |
- |