#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <utility>
#include <vector>
#include "wiring.h"
using namespace std;
map < pair < int ,int > , long long > DP;
long long F(int N,int M,vector < int > &r,vector < int > &b)
{
if(N==0&&M==0) return abs(r[N]-b[M]);
else
{
if(DP.find(make_pair(N,M))!=DP.end()) return DP[make_pair(N,M)];
long long t=abs(r[N]-b[M]),x=1e18;
if(N) x=min(x,F(N-1,M,r,b));
if(M) x=min(x,F(N,M-1,r,b));
if(N&&M) x=min(x,F(N-1,M-1,r,b));
//printf("%d %d %lld\n",N,M,t+x);
DP[make_pair(N,M)]=t+x;
return t+x;
}
}
long long min_total_length(vector < int > r, vector < int > b)
{
int N=r.size();
int M=b.size();
int i;
long long ans=0;
if(N<=200&&M<=200) return F(N-1,M-1,r,b);
for(i=0;i<N;i++) ans+=abs(r[i]-b[0]);
for(i=0;i<M;i++) ans+=abs(b[i]-r[0]);
ans-=min(abs(b[0]-r[M]),abs(b[N]-r[0]));
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
45 ms |
2808 KB |
Output is correct |
8 |
Correct |
48 ms |
2808 KB |
Output is correct |
9 |
Correct |
53 ms |
2936 KB |
Output is correct |
10 |
Correct |
46 ms |
2808 KB |
Output is correct |
11 |
Correct |
60 ms |
2936 KB |
Output is correct |
12 |
Correct |
49 ms |
2936 KB |
Output is correct |
13 |
Correct |
54 ms |
2936 KB |
Output is correct |
14 |
Correct |
51 ms |
2812 KB |
Output is correct |
15 |
Correct |
47 ms |
2824 KB |
Output is correct |
16 |
Correct |
46 ms |
2812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
20 ms |
1536 KB |
Output is correct |
3 |
Incorrect |
24 ms |
1528 KB |
3rd lines differ - on the 1st token, expected: '41596985758595', found: '74876454164532' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Incorrect |
35 ms |
2040 KB |
3rd lines differ - on the 1st token, expected: '1068938599', found: '100056491257580' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
26 ms |
2036 KB |
3rd lines differ - on the 1st token, expected: '373710605', found: '19567789201' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
45 ms |
2808 KB |
Output is correct |
8 |
Correct |
48 ms |
2808 KB |
Output is correct |
9 |
Correct |
53 ms |
2936 KB |
Output is correct |
10 |
Correct |
46 ms |
2808 KB |
Output is correct |
11 |
Correct |
60 ms |
2936 KB |
Output is correct |
12 |
Correct |
49 ms |
2936 KB |
Output is correct |
13 |
Correct |
54 ms |
2936 KB |
Output is correct |
14 |
Correct |
51 ms |
2812 KB |
Output is correct |
15 |
Correct |
47 ms |
2824 KB |
Output is correct |
16 |
Correct |
46 ms |
2812 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
20 ms |
1536 KB |
Output is correct |
19 |
Incorrect |
24 ms |
1528 KB |
3rd lines differ - on the 1st token, expected: '41596985758595', found: '74876454164532' |
20 |
Halted |
0 ms |
0 KB |
- |