#include "wiring.h"
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <cmath>
#include <numeric>
#include <limits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, int> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
// #define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define nl endl
#define S second
#define lb lower_bound
#define ub upper_bound
#define aint(x) x.begin(), x.end()
#define raint(x) x.rbegin(), x.rend()
#define ins insert
const int MOD = 1000000007;
long long min_total_length(vi r, vi b) {
int n = r.size(), m = b.size();
vvl dp(m,vl(n,INT_MAX));
ll ans = 0;
dp[0][0] = abs(r[0]-b[0]);
FOR(i,1,m){
dp[i][0] = dp[i-1][0] + ll(abs(r[0]-b[i]));
}
vi min_dist(n,INT_MAX);
FOR(i,0,n){
int pos = lower_bound(b.begin(),b.end(),r[i]) - b.begin();
if(pos<m)
min_dist[i] = min(min_dist[i],(abs(r[i]-b[pos])));
pos = upper_bound(b.begin(),b.end(),r[i]) - b.begin();
if(pos<m)
min_dist[i] = min(min_dist[i],(abs(r[i]-b[pos])));
}
// FOR(i,0,n)cout << min_dist[i] << " ";
// cout << endl;
FOR(i,1,n){
dp[0][i] = dp[0][i-1]+min_dist[i];
FOR(j,1,m){
dp[j][i] = min(dp[j][i],dp[j][i-1] + min_dist[i]);
dp[j][i] = min(dp[j][i],dp[j-1][i] + abs(r[i]-b[j]));
dp[j][i] = min(dp[j][i],dp[j-1][i-1] + abs(r[i]-b[j]));
}
}
// cout << endl;
// FOR(i,0,m){
// FOR(j,0,n){
// cout << dp[i][j] << " ";
// }cout << endl;
// }
return dp[m-1][n-1];
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(vi, vi)':
wiring.cpp:46:5: warning: unused variable 'ans' [-Wunused-variable]
46 | ll ans = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '26187' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Runtime error |
144 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '17703', found: '18114' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
122 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '26187' |
2 |
Halted |
0 ms |
0 KB |
- |