# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1062325 | Ausp3x | 전선 연결 (IOI17_wiring) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 人外有人,天外有天
// author: Ausp3x
#pragma GCC optimize("O1, O2, O3, Ofast, unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include "wiring.h"
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define pb push_back
#define DEBUG
typedef long long lng;
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int const INF32 = 0x3f3f3f3f;
lng const INF64 = 0x3f3f3f3f3f3f3f3f;
vector<int> st1_R, st1_B;
vector<vector<lng>> memo;
lng st1_dp(int i, int j) {
if (i < 0 || j < 0)
return INF64;
if (memo[i][j] != INF64)
return memo[i][j];
if (i == 0 && j == 0)
return memo[i][j] = abs(st1_R[i] - st1_B[j]);
return memo[i][j] = min(min({st1_dp(i - 1, j), st1_dp(i, j - 1), st1_dp(i - 1, j - 1)}) + abs(st1_R[i] - st1_B[j]), INF64);
}
lng min_total_length(vector<int> R, vector<int> B) {
int n = R.size(), m = B.size();
if (n <= 200 && m <= 200) {
st1_R = R;
st1_B = B;
memo.resize(n, vector<lng>(m, INF64));
#ifdef DEBUG
cout << st1_dp(n - 1, m - 1) << endl;
#endif DEBUG
#ifndef DEBUG
return st1_dp(n - 1, m - 1);
#endif
}
if (R.back() < B[0]) {
lng ans = 0;
for (int i = 0; i < max(n, m); i++) {
ans += B[min(i, m - 1)] - R[min(i, n - 1)];
// cout << ans << ' ';
}
// cout << endl;
return ans;
}
return -1;
}
#ifdef DEBUG
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<int> R(n);
for (int &r : R)
cin >> r;
vector<int> B(m);
for (int &b : B)
cin >> b;
cout << min_total_length(R, B) << endl;
}
return 0;
}
#endif